title: Yii2: compare validation of password and repassword
body: <p>I'm a beginner of Yii, that's why can't figure out, what's wrong.
  I use the same model - <strong>Users</strong> - for login, registration and edition profile.</p>
  
  <p><em>Users model</em>:</p>
  
  <pre><code>class Users extends \yii\db\ActiveRecord {
      public $password_repeat; //have no this value in DB, that's why write it as property directly. Need only for profile editing
  
      public function rules() {
          return [
              [['username', 'password', 'authKey', 'accessToken'], 'safe'],
              [['username', 'password'], 'required'],
              [['username', 'password', 'authKey', 'accessToken'], 'string', 'max' =&gt; 255],
              [['is_admin'], 'boolean'],
  
  //don't use scenarios() here. Use 'on' instead 
              ['password_repeat', 'required', 'on' =&gt; 'update'],
              ['password_repeat', 'compare', 'compareAttribute'=&gt;'password', 'message'=&gt;"Passwords don't match", 'on' =&gt; 'update' ],        
          ];
      }
  }
  </code></pre>
  
  <p><em>view snippet</em>:</p>
  
  <pre><code>&lt;?php $form = ActiveForm::begin([
      'id' =&gt; 'data-form',
      'options' =&gt; ['class' =&gt; 'form-horizontal', 'enctype' =&gt; 'multipart/form-data'],
      'fieldConfig' =&gt; [
          'template' =&gt; "{label}\n&lt;div class=\"col-lg-3\"&gt;{input}&lt;/div&gt;\n&lt;div class=\"col-lg-6\"&gt;{error}&lt;/div&gt;",
          'labelOptions' =&gt; ['class' =&gt; 'col-lg-1 control-label'],
      ],
  ]); ?&gt;
  
  &lt;?= $form-&gt;field($user_data, 'username') ?&gt;
  
  &lt;?= $form-&gt;field($logo_data, 'imageFile')-&gt;fileInput() ?&gt;
  
   &lt;?= $form-&gt;field($user_data, 'password')-&gt;passwordInput(['value'=&gt;''])?&gt;
   &lt;?= $form-&gt;field($user_data, 'password_repeat')-&gt;passwordInput(['value'=&gt;''])?&gt;
  
   &lt;?= Html::submitButton('Zapisz', ['class' =&gt; 'btn btn-primary btn-block', 'name' =&gt; 'data-button']) ?&gt;
  
  &lt;?php ActiveForm::end(); ?&gt;
  </code></pre>
  
  <p><em>Site controller action</em>:</p>
  
  <pre><code>public function actionProfile(){
      //3 model instances to retrieve data from users &amp;&amp; company_profiles and logo
      $user_data = Users::find()-&gt;where(['id'=&gt;Yii::$app-&gt;user-&gt;id])-&gt;one();
      $company_profile_data = CompanyProfiles::find()-&gt;where(['user_id'=&gt;Yii::$app-&gt;user-&gt;id])-&gt;one();
      $logo_data = new UploadLogo();
  
      //here I upload file through $logo_data
  
      if (isset($_POST['Users'])){
          $user_data-&gt;username = $_POST['Users']['username'];
          $company_profile_data-&gt;firm_data = $_POST['CompanyProfiles']['firm_data'];
          //other assignments
  
          $user_data-&gt;scenario = 'update'; //specially for 'profile' page           
  
          if (($_POST[Users][password] !== '') &amp;&amp; ($_POST[Users][password_repeat]) !== '' &amp;&amp; ($_POST[Users][password] == $_POST[Users][password_repeat]))
             $user_data-&gt;password_repeat = $user_data-&gt;password = md5($_POST[Users][password]); //MAYBE, problem is here?
  
          $user_data-&gt;update();
          $company_profile_data-&gt;update();
      }
  
      return $this-&gt;render('profile', ['user_data' =&gt; $user_data, 'company_profile_data' =&gt; $company_profile_data, 'logo_data' =&gt; $logo_data]);
  }
  </code></pre>
  
  <p>I use <em>update</em> scenario for profile editing page. During this scenario password_repeat is required and need to be the same as password. But it doesn't work as expected.
  <em>A:</em> When i try to submit form without entered password AND repass, I see message <em>"can't be blank"</em> only for password. Then I put value in ONLY pass input, and page reloads (DB table doesn't changes), and after reloading I see message <em>"can't be blank"</em> this time for repass field! But the most strange is that before next page reloading everything work normal. After reloading <em>goto A;</em> :)
  If I delete scenario, everything is ok with validation. But I can't delete it, cause in this case registration fails (repass field becomes required to fill).</p>
  
  <p>So, what need I to do my with pass-repass functionality to work it fine (with scenario 'update')? Where is mistake?</p>