【问题标题】:set rules for array input field in Yii在 Yii 中为数组输入字段设置规则
【发布时间】:2019-06-12 15:45:25
【问题描述】:

我有一个字段,我可以在单击“+”按钮时添加多行。但我想在 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="">

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

【问题讨论】:

标签: validation yii yii2


【解决方案1】:

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

在规则()中

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-10
    • 2021-10-03
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多