为Yii中的数组输入字段设置规则

时间:2019-06-12 15:45:25

标签: validation yii yii2

我有一个字段,单击“ +”按钮可以在其中添加多行。但是我想以Yii验证程序的形式设置所需的规则。

['input_field_name','each','rule'=> ['required']]

我有此输入字段

<input type="number" class="form-control reqInput input-unchanged" name="Domains[input_name][0][phone]">
<input type="number" class="form-control reqInput input-unchanged" name="Domains[input_name][1][phone]" value="">
<input type="number" class="form-control reqInput input-unchanged" name="Domains[input_name][2][phone]" value="">

我想要每个输入字段必填的规则。

1 个答案:

答案 0 :(得分:-1)

您可以为此创建自己的验证器。

在rules()

    return [
        // an inline validator defined as the model method validateCountry()
        ['country', 'validateCountry'],
    ];

在模型中添加新功能:

public function validateCountry($attribute, $params, $validator)
{
    //create you custom logic here, loop throughout an array and check the 
    //values, the code below is just example
    if (!in_array($this->$attribute, ['USA', 'Indonesia'])) {
        $this->addError($attribute, 'The country must be either "USA" or 
        "Indonesia".');
    }
}
相关问题