yii\widgets\ActiveForm::validateMultiple PHP Method

validateMultiple() public static method

This is a helper method that simplifies the way of writing AJAX validation code for tabular input. For example, you may use the following code in a controller action to respond to an AJAX validation request: php ... load $models ... if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validateMultiple($models); } ... respond to non-AJAX request ...
public static validateMultiple ( array $models, mixed $attributes = null ) : array
$models array an array of models 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.
return array the error message array indexed by the attribute IDs.
    public static function validateMultiple($models, $attributes = null)
    {
        $result = [];
        /* @var $model Model */
        foreach ($models as $i => $model) {
            $model->validate($attributes);
            foreach ($model->getErrors() as $attribute => $errors) {
                $result[Html::getInputId($model, "[{$i}]" . $attribute)] = $errors;
            }
        }
        return $result;
    }

Usage Example

Example #1
1
 public function update(AdminAction $adminAction)
 {
     /**
      * @var $model CmsTree
      */
     $model = $this->model;
     $relatedModel = $model->relatedPropertiesModel;
     $rr = new RequestResponse();
     if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) {
         $model->load(\Yii::$app->request->post());
         $relatedModel->load(\Yii::$app->request->post());
         return \yii\widgets\ActiveForm::validateMultiple([$model, $relatedModel]);
     }
     if ($rr->isRequestPjaxPost()) {
         $model->load(\Yii::$app->request->post());
         $relatedModel->load(\Yii::$app->request->post());
         if ($model->save() && $relatedModel->save()) {
             \Yii::$app->getSession()->setFlash('success', \Yii::t('skeeks/cms', 'Saved'));
             if (\Yii::$app->request->post('submit-btn') == 'apply') {
             } else {
                 return $this->redirect($this->indexUrl);
             }
             $model->refresh();
         } else {
             $errors = [];
             if ($model->getErrors()) {
                 foreach ($model->getErrors() as $error) {
                     $errors[] = implode(', ', $error);
                 }
             }
             \Yii::$app->getSession()->setFlash('error', \Yii::t('skeeks/cms', 'Could not save') . $errors);
         }
     }
     return $this->render('_form', ['model' => $model, 'relatedModel' => $relatedModel]);
 }
All Usage Examples Of yii\widgets\ActiveForm::validateMultiple