title: custom validation in yii2 model
body: <p>I am trying to add custom rule to form. I have added a custom function in model but it's not working for me.</p>
  
  <pre><code>class BackendUser extends ActiveRecord implements IdentityInterface
  {
  public function rules()
  {
  
      return [
  
          ['username','validateUsername','params'=&gt;'username'=&gt;'username']],
  
  
      ];
  }
  
  public function validateUsername($attribute, $params)
  {
      if (preg_match('/[^a-z])/i', $this-&gt;$attribute)) {
          $this-&gt;addError($attribute, 'Username should only contain
      alphabets');
      }
  
  }}
  </code></pre>