Ad hoc Validation

To validate values that are not bound to any model, we need to use ad hoc validation. Or if you want to validate values without models then use ad hoc validation.

To perform one type of validation, you may call yii\validators\validator() method of the desired validator.

Not all the valdiators support this type of validation.

For example, we are calling yii\validators\DateValidator()

$date = '24th November 2016, Thursday';
 $validator = new yii\validators\DateValidator();

 if ($validator->validate($date, $error)) {
     echo 'Date is valid.';
 } else {
     echo $error;

But to perform multiple validations, against several values you can use yii\base\DynamicModel which defines both attributes and rules.

For example, here we are calling DynamicModel to validate date and email.

public function actionAdHocValidation() { 
    $model = DynamicModel::validateData([ 
       'date' => '24th November 2016, Thursday', 
       'email' => '[email protected]' 
    ], [ 
       [['date', 'email'], 'string', 'max' => 40], 
    if ($model->hasErrors()) { 
    } else { 
       echo "success"; 

On the browser, pass the URL


YII Ad hoc validation 1

Look at the above output, both date and email have been validated at the same time.