yii2 Disable Validation Error Message On Focus / Key Up


Example

By default error message appears below textbox in <div class="help-block"></div> on keyUp or after pressing submit button if any validation constraints aren't met.

Sometimes we want a message on submit only i.e. no validation at onKeyup event.

Let's check yii2/widgets/ActiveForm.php file:

<?php

namespace yii\widgets;

use Yii;
use yii\base\InvalidCallException;
use yii\base\Widget;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\helpers\Json;

class ActiveForm extends Widget
{
  public $action = '';
  public $method = 'post';
  public $options = [];
  .
  .
  .
  public $validateOnSubmit = true;
  public $validateOnChange = true;
  public $validateOnBlur = true;
  public $validateOnType = false;
  
  .
  .
  .
  
}

There we see that $validateOnBlur is set to true by default. Changing framework files is a very bad thing to do so we need to override it when using the form:

<?php $form = ActiveForm::begin([ 'id' => 'register-form','validateOnBlur' => false]); ?>