Skip to content
Advertisement

How to handle validation of multiple hidden fields

I have created a form where in many cases some fields are not displayed until user select a certain option that will make the particular field displayed, this is controlled by HTML onchange Event Attribute with functions in script. To this point everything works fine.

Problem:

In the Request class I am implementing some rules for validation , for all displayed fields and for particular none displayed fields if only their options are selected then they should be validated. In my case I have 8 of this none displayed fields are a_text, b_text, c_text, d_text, e_text, f_text,g_text, h_text. If this fields were 2 it would be easy to write an If statement to choose which should be validated and which should not be validated like below:

JavaScript

In my case I have 8 fields which are none displayed, of which I have to handle all cases of fields if it’s selected and not, this will lead to 100’s or a lot of if…elseif..statements. What is the wright approach to handle this kind of task.

Form

JavaScript

My question: what is the wright approach to handle the above scenario without writing 100’s of if..elseif statements. Thanks in advance.

Advertisement

Answer

In form validation u have rules like required with or without, u can see more here

Little bit lower on the docs u can see Conditionally Adding Rules

So basically u will be doing same validation but without too much mess in your code.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement