Yii2 check array of inputs

I have the following data:

Array ( [category] => Array ( [0] => d [1] => 100 [2] => 100 [3] => 100 ) [volume] => Array ( [0] => 100 [1] => 100 [2] => 100 ) [urgency] => Array ( [0] => 100 [1] => 100 [2] => 100 ) [importance] => Array ( [0] => 100 [1] => 100 [2] => 100 ) ) 

And I created a DynamicModel for it with the rules "every value must be integer" (added in 2.0.4).

 $view_model = DynamicModel::validateData(compact('category', 'volume', 'urgency', 'importance'), [ [['category', 'volume', 'urgency', 'importance'], 'each', 'rule' => ['integer']], ]); 

I have:

  <?= $form->field($model, 'category[0]')->textInput() ?> <?= $form->field($model, 'category[1]')->textInput() ?> <?= $form->field($model, 'category[2]')->textInput() ?> ... <?= $form->field($model, 'importance[2]')->textInput() ?> 

The problem is that when I submit the form with "d" on the first input, I have errors for each category entry: enter image description here

What am I doing wrong?

+6
source share
2 answers

You can use each validator Info: This validator is available from version 2.0.4.

 [ // checks if every category ID is an integer ['categoryIDs', 'each', 'rule' => ['integer']], ] 

This validator only works with an array attribute. It checks whether each element of the array can be successfully verified using the specified validation rule. In the above example, the categoryID attribute must take the value of the array, and each element of the array will be checked by the integer validation rule.

 rule: an array specifying a validation rule. The first element in the array specifies the class name or the alias of the validator. The rest of the name-value pairs in the array are used to configure the validator object. allowMessageFromRule: whether to use the error message returned by the embedded validation rule. Defaults to true. If false, it will use message as the error message. Note: If the attribute value is not an array, it is considered validation fails and the message will be returned as the error message. 
+2
source

This answer applies to ajax request scripts

In your controller

 use yii\base\Model; use yii\widgets\ActiveForm; use yii\web\Response; public function actionCreate() { $models = [ 'model1' => new Category, 'model2' => new Category, ]; if (Yii::$app->request->isAjax && Model::loadMultiple($models, Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; $validate = []; $validate = array_merge(ActiveForm::validateMultiple($models), $validate); // If you need to validate another models, put below. // $validate = array_merge(ActiveForm::validate($anotherModel), $validate); return $validate; } if (Model::loadMultiple($models, Yii::$app->request->post())) { foreach($models as $key => $model) { $model->save(); } } return $this->render('create', [ 'models' => $models, ]); } 

In your opinion

 <?php $form = ActiveForm::begin([ 'enableClientValidation' => false, 'enableAjaxValidation' => true, ]); ?> <?= $form->field($models['model1'], '[model1]name')->textInput(); ?> <?= $form->field($models['model2'], '[model2]name')->textInput(); ?> <?= Html::submitButton('Create') ?> <?php ActiveForm::end(); ?> 
0
source

All Articles