I have a form with a series of controls nested within it, and although I can get validation for the entire form to work, I need to validate different parts of the form at different times.
Say for example I have a form asking a user to fill out their personal details (name, address, etc.), but in the form there is option to include a number of addresses. I need to be able to validate the form as a whole (addresses are not mandatory), but also separately validate the form that controls the input for the secondary addresses (i.e. when filling out a secondary address a user must enter a street, zip code, etc). It will only be one form for the addresses, and each entered address will use the values to build up a string in a <li></li>
.
I have done this before easily enough using Telerik Kendo whereby you can specify which containers to validate regardless of whether they are nested or not, so you can validate the main container and the child container separately, but I am just not sure how to do this with jQuery validate. I also know nested forms is a standards violation, so I am not entirely sure how to approach this.
So essentially I need to create this sort of structure, where the button to save form validates that the user has specified at least a name and date of birth, but if adding an address that the user must enter the address details.
- Name (mandatory)
- Date of birth (mandatory)
- Addresses (optional)
----- [form to add addresses]
------ Address Line (mandatory)
------ City (mandatory)
------ State (mandatory)
------ Zip (mandatory)
------ Button to add address
- Gender (optional)
- Age (optional)
- Weight (optional)
- Button to save form