0

I have a form with many fields with the same name, this will be an array input for PHP.

<input type="text" name="user[]" required />

Unfortunately it seems that Jquery validator, used on entire form, ignores consecutive inputs with the same name and it validate only the first one.

Take a look at this example:

<form class="validation-form">
    <lable>User1</lable>    
    <input type="text" name="user[]" required /><br>
    <lable>User2</lable>
    <input type="text" name="user[]" required /><br>
    <button class="submit-form">submit</button>
</form>

https://jsfiddle.net/xr5a03qf/

If you push on submit, you will se only the first required field is checked, if you fill just the first one, the form will be submitted as valid.

Is this a bug?

Tobia
  • 9,165
  • 28
  • 114
  • 219
  • This question appears to have a solution: [using jquery validate with multiple fields of the same name](https://stackoverflow.com/questions/2589670/using-jquery-validate-with-multiple-fields-of-the-same-name) – freedomn-m Aug 18 '22 at 10:50
  • So is not possibile to use attribute only? – Tobia Aug 18 '22 at 13:24
  • The [answer](https://stackoverflow.com/a/69452906/2181514) on that question suggests that jquery validate caches the names and only applies the validation to the first. This appears to be a specific decision rather than a bug. You can either remove the cache line from jquery-validate or add new rules by looping the inputs yourself. Both solutions provided on the question linked above. – freedomn-m Aug 18 '22 at 14:26
  • Yes, that's how it works as verified in the documentation. The `name` on each field must be unique. Otherwise, only the first element matching the `name` is validated and the rest are ignored. – Sparky Aug 22 '22 at 22:33

0 Answers0