0

I have field where I can add multiple row on click "+" button. But I want to set required rules in Yii validator form.

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

I have this input field

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

I want required rules for each input field.

Anuj Burnwal
  • 41
  • 1
  • 7
  • 1
    Possible duplicate of [Yii2: validation rule for array?](https://stackoverflow.com/questions/27252934/yii2-validation-rule-for-array) – user206 Jun 12 '19 at 16:25
  • How do you submit your form? Is your field presented as array (input_field_name[]) when the receiving script parses the request? – Michael Krutikov Jun 13 '19 at 07:30

1 Answers1

-1

You can create your own validator for this.

in rules()

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

add new function in your model:

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".');
    }
}
Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16