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



我有字段,我可以在单击"+"按钮上添加多行。但是我想以 Yii 验证器的形式设置所需的规则。

["input_field_name"、"每个"、"规则" => ["必需"]]

我有这个输入字段

<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="">

我想要每个输入字段所需的规则。

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

在规则((

    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".');
    }
}

最新更新