validating array of inputs

Hello,

How can I handle activeForm errors for inputs with the same name:


<input type="text" name="item[1]">

<input type="text" name="item[2]">

<input type="text" name="item[3]">

"Each" validator returns error message for all inputs and I need different errors for each input

Here is very similar question but without concrete answer

stackoverflow[dot]com/questions/33198445/yii2-validating-array-of-inputs

you have inputs which performs different actions with a same name? what’s that mess?

IMHO you cant create particular validator for each of these fields when they have same name

Try to create different names for them.

Also why you don’t use yii2 form builder?

Here is link with question

stackoverflow[dot]com/questions/33198445/yii2-validating-array-of-inputs

I use yii2 form builder

I add new inputs by jquery and each input field have name="Model[item][]" , then I check it by EachValidator like this:


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

where $item is array but problem is: I get back 1 error message for all $item array but I need error message for eache element of the array (for each input field)

when you add a error for a name this error will be assigned for each input with that name. looks like you need to check it without yii validator, but it is bad solution i guess.

maybe a little crunchy, but you can create name for each item with counter (like name1, name2, etc)

and create custom validator assigning for each name.

Suppose I can create name for each item with counter in my View but What about Model attributes?

You are right. Looks like i can’t help you. Will wait someone who done this before.

Hi,

i think you can do it with yii2.

First give the ‘hidden’ value to your concerned inputs instead of adding them with Jquery (hiddenInput() method)

And you will make them visible with jquery by changing the input type.

In your Model you should use the ‘when’ property to specify the conditions on which the rules should be applied. For exemple your field will be required when a checkbox is checked.

Notice that yii2 applied rules on hidden fields.

Hope it will help, i never tried all of this myself but it should work. Look at the yii2 guide on the Conditional validation section it will help you.

I think I must do it with help of "Collecting tabular input".

I cant do it in other way ((