Yii2 validation rule for multiple inputs with same name
我有一个表单,它有多个同名的输入,这些输入是使用 jQuery 动态添加的。输入名称如下:
1 2 | ModelName[dynamic_name][] ModelName[dynamic_name][] |
我还在
由于您的
例如,检查每个元素是否为整数:
1 | [['dynamic_name'], 'each', 'rule' => ['integer']], |
yii2,你可以使用
查看
1 2 3 4 | <?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'dynamic_name[]')->textInput() ?> <?= Html::submitButton('Submit', ['class' => 'btn', 'name' => 'hash-button']) ?> <?php ActiveForm::end(); ?> |
型号
1 2 3 4 5 6 7 8 9 10 11 | class MyModel extends Model { public $dynamic_name = []; public function rules() { return [ // checks if every dynamic_name is an integer ['dynamic_name', 'each', 'rule' => ['integer']], ] } } |
Note: This validator will not work with inline validation rules in case of usage outside the model scope, e.g. via validate() method.
链接:http://www.yiiframework.com/doc-2.0/yii-validators-eachvalidator.html