установить правила для поля ввода массива в Yii - PullRequest
0 голосов
/ 12 июня 2019

У меня есть поле, в которое я могу добавить несколько строк по нажатию кнопки «+».Но я хочу установить необходимые правила в форме валидатора 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 голосов
/ 13 июня 2019

Вы можете создать свой собственный валидатор для этого.

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