title: Using custom validators with ActiveForm in Yii2
body: <p>I want to make custom validation function like built-in validation <code>required</code>. I have example code here: </p>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>use yii\base\Model;
  
  class TestForm extends Model
  {
      public $age;
      public function rules(){
          return [
              ['age', 'my_validation']
          ];
      }
      public function my_validation(){
          //some code here
      }
  }
  </code></pre>
  
  <p><strong>View:</strong></p>
  
  <pre><code>&lt;?php
  
  use yii\helpers\Html;
  use yii\widgets\ActiveForm;
  
  
  $this-&gt;title = 'test';
  ?&gt;
  &lt;div style="margin-top: 30px;"&gt;
  
      &lt;?php $form = ActiveForm::begin(); ?&gt;
      &lt;?= $form-&gt;field($model, 'age')-&gt;label("age") ?&gt;
      &lt;div class="form-group"&gt;
          &lt;?= Html::submitButton('submit', ['class' =&gt; 'btn btn-primary']) ?&gt;
      &lt;/div&gt;
      &lt;?php ActiveForm::end(); ?&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>use app\models\form\TestForm;
  use yii\web\Controller;
  
  class TestController extends Controller
  {
      public function actionIndex(){
          $model = new TestForm();
  
          if($model-&gt;load(\Yii::$app-&gt;request-&gt;post())){
              return $this-&gt;render('test', array(
                  'model'=&gt;$model,
                  'message'=&gt;'success'
              ));
          }
          return $this-&gt;render('test', array('model'=&gt;$model));
      }
  }
  </code></pre>
  
  <p>in this example I have a field for age and this <code>my_validation</code> function should check if age is over 18 before submit and throw error if age is under 18. This validation should be processed by ajax like it is in case of <code>required</code>  rule if you try to submit empty field. </p>