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.

2
  • 1
    Possible duplicate of Yii2: validation rule for array? Commented Jun 12, 2019 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? Commented Jun 13, 2019 at 7:30

1 Answer 1

-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".');
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.