0

I'm using validate.js in react to validate a form. In parts of the form, fields can be added dynamically. For example like phone numbers.

How do I create validations for each of the fields that are newly created or already exist? This is as far as I got.

phonenumbers: {
 type: 'array'
}
Megaman
  • 25
  • 8
  • can you iterate over each phone number and validate? this.phoneNumbers.map((item, key) => validate(item, constraints); ); – PeonProgrammer Nov 06 '19 at 15:34
  • That's a good idea. The rest of the form has an error element which captures the result of the validation. I guess I can append the array errors into this one or create another error element to store these errors. Will try it, thanks. – Megaman Nov 06 '19 at 16:43
  • 1
    I tried your suggestion and it worked, thanks. I also had to take into account deleting/adding rows and updating errors array respectively. – Megaman Nov 08 '19 at 08:23
  • validate.js has an example on how to do just that https://jqueryvalidation.org/files/demo/dynamic-totals.html – luenib Jul 18 '23 at 16:32

1 Answers1

0

You could iterate over each phone number and validate. Something like :

this.phoneNumbers.map((item, key) => validate(item, constraints););
PeonProgrammer
  • 1,445
  • 2
  • 15
  • 27