yii\widgets\ActiveForm::validate PHP Метод

validate() публичный статический метод

This is a helper method that simplifies the way of writing AJAX validation code. For example, you may use the following code in a controller action to respond to an AJAX validation request: php $model = new Post; $model->load(Yii::$app->request->post()); if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); } ... respond to non-AJAX request ... To validate multiple models, simply pass each model as a parameter to this method, like the following: php ActiveForm::validate($model1, $model2, ...);
public static validate ( Model $model, mixed $attributes = null ) : array
$model yii\base\Model the model to be validated.
$attributes mixed list of attributes that should be validated. If this parameter is empty, it means any attribute listed in the applicable validation rules should be validated. When this method is used to validate multiple models, this parameter will be interpreted as a model.
Результат array the error message array indexed by the attribute IDs.
    public static function validate($model, $attributes = null)
    {
        $result = [];
        if ($attributes instanceof Model) {
            // validating multiple models
            $models = func_get_args();
            $attributes = null;
        } else {
            $models = [$model];
        }
        /* @var $model Model */
        foreach ($models as $model) {
            $model->validate($attributes);
            foreach ($model->getErrors() as $attribute => $errors) {
                $result[Html::getInputId($model, $attribute)] = $errors;
            }
        }
        return $result;
    }

Usage Example

Пример #1
3
 /**
  * Performs ajax validation.
  *
  * @param \yii\base\Model $model
  *
  * @throws \yii\base\ExitException
  */
 protected function performValidationModel(Model $model)
 {
     if ($model->load(Yii::$app->request->post())) {
         return ActiveForm::validate($model);
     }
     return;
 }
All Usage Examples Of yii\widgets\ActiveForm::validate