title: Yii2: ActiveForm: combine rules / multiple validation on one field
body: <p>LoginForm:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function rules()
  {
      return [
          // username and password are both required
          [['username', 'password'], 'required'],
          // username should be a number and of 8 digits
          [['username'], 'number', 'message'=&gt;'{attribute} must be a number'],
          [['username'], 'string', 'length' =&gt; 8],
          // password is validated by validatePassword()
          ['password', 'validatePassword'],
      ];
  }
  
  /**
   * Validates the password.
   * This method serves as the inline validation for password.
   *
   * @param string $attribute the attribute currently being validated
   * @param array $params the additional name-value pairs given in the rule
   */
  public function validatePassword($attribute, $params)
  {
      if (!$this-&gt;hasErrors()) {
          $user = $this-&gt;getUser();
          if (!$user || !$user-&gt;validatePassword($this-&gt;password)) {
              $this-&gt;addError($attribute, 'Incorrect username or password.');
          }
      }
  }
  </code></pre>
  
  <p>I have set up 2 rules for the same field as you can see above:</p>
  
  <pre class="lang-php prettyprint-override"><code>[['username'], 'number', 'message'=&gt;'{attribute} must be a number'],
  [['username'], 'string', 'length' =&gt; 8],
  </code></pre>
  
  <p>I would like the form to display different error messages for the following 3 <s>scenarios</s> <em>situations</em>:</p>
  
  <ol>
  <li>The provided value is neither a number, nor 8 characters (digits).</li>
  <li>The provided value is a number, but is not of 8 characters (digits).</li>
  <li>The provided value is not a number, but is of 8 characters (digits).</li>
  </ol>
  
  <p>My question is 2 fold:</p>
  
  <p>A. Is there a way to combine these rules in any standard, <code>Yii2</code> way.<br>
  B. <a href="https://stackoverflow.com/q/33071345/2037924">In my previous question</a> I have tried to set up a custom validator (the obvious way to solve this), but it was very simply ignored. The only way I could make it validate was if I added the <code>username</code> field to a scenario. However, once I added <code>password</code> too, it was again ignored. Any reason's for this that you can think of? <strong>EDIT</strong>: <code>skipOnError = false</code> changed nothing at all in this behaviour.</p>
  
  <p>So please, when you answer, make sure you test it preferably in <code>yii2/advanced</code>; I barely touched the default set up, so it should be easy to test.</p>
  
  <p><strong>EDIT</strong>: for clarity, I would like to only allow numbers that are of 8 characters (digits), so they can potentially have a leading <code>0</code>, eg. <code>00000001</code>, or <code>00000000</code> for that matter. This is why it has to be a numeric string.</p>