Custom Form - Required field only when option in list is selected


(David Herrington) #1

I have a multi page form and one of the pages looks like this:

 

1 x Option list question type with two options: Option 1 and Option 2

 

Selecting Option 1 allows you to move forward to the next page in the form. Selecting Option 2 shows a hidden div with two additional text questions. 

 

What I want is for the two text fields to become a required entry only when Option 2 is selected. Does anyone know how to achieve this in Matrix or am I required to write some custom script?

 


(Nic Hubbard) #2

Yes, Matrix can do this. For the two text fields to become conditionally required, go to the Simple Validation Rules section. Set the questions to Required then click the "Add a condition to this validation rule?", then submit. This will then let you choose another question to base the validation off of. Choose your option list question and set the regex option and put in the value.

 

I have done this with some really complex forms and it works great.


(David Herrington) #3

As ever, thanks Nic for the reply.

So I have three options in my option list. Matrix pumps out the ID and values for these options automatically as:


q(assetID)_q1_0

q(assetID)_q1_1

q(assetID)_q1_2


So what do I put in the regex box?


Thanks for your help


(Nic Hubbard) #4

So what do I put in the regex box?

 

In the regex box you would want to match the answer of the option that is suppose to expand the two conditionally required fields.

 

Remember, if can be a regex, so any of the regex operators can be used.


(David Herrington) #5

So if my 3 options in my option list are Yes, No and Maybe (where Maybe is the option once clicked shows the two hidden questions) do I just write Maybe in the regex box?

 

Sorry to sound like a complete n00b so thanks for staying with me Nic.


(Nic Hubbard) #6

Yes you would. But make sure to choose the "matches a regex" option.

 

Also, see the manuals page: https://manuals.matrix.squizsuite.net/custom-form/chapters/questions#adding-conditions


(David Herrington) #7

I must be doing something wrong but can't work out where? I've entered Maybe as the regex and regardless of what option I chose in the option list, Matrix tells me to complete the required field.

 

Just to prove i'm not going mad, I added another text question and changed the condition to say "Check the new text question I created and use a regex of 1.

 

If I type 1 in the new text area and submit to the next page in the form, BINGO, Matrix tells me to fill in the required question. Anything other than 1 allows me to leave that text question blank and move on in the form. 


(Nic Hubbard) #8

Ah, it is because you are using an option list, which sets the "value" different from the label, as a number. So change your regex to the position that Maybe is.

 

So, if "Maybe" is the third option, enter 2 for the regex since numbering starts at 0. 

 

I just tested this and it works as expected. Sorry that I hadn't realized you were using an option list before.


(David Herrington) #9

Thanks Nic. I'm glad i'm not going mad because I was doing exactly that; typing "2" as the regex for the select list.

 

I've done loads of testing over the weekend and what I found out is that if I create a section and add the option list question first and then 3 text questions, it doesn't work. Matrix continues to tell me to the option question is a required field. If however I create the text questions first and then the option list question, add the validation and test it does work. Maybe there's a bug or something up with my install?

 

I'm running 5.1.3 so upgrading may fix that issue. Either way I've worked out how to get around it for now which fixes my issue.

 

Thanks for all your help over the last few days.


Bug with Condition under Simple Validation Rules?
(Joy) #10

I have the same issue, I have a tickbox list which shows/hides three sections. I want the fields in each section to be 'required' only when the corresponding tickbox in the tickbox list is ticked.

I've tried the 'Add a condition to the validation rule' to the fields in each section but still no success with the regex.

Then I deleted the options list and re-added it as per the last post and still not working.

What do I need to put in the regex?

 

Does anyone have any ideas? I'm using 5.2.6.1

 

Thanks


(Nic Hubbard) #11

Do you have the page that we can look at?


(Joy) #12

Here is a test page, I've disabled the show/hide for now to test the validation

https://www.qcaa.qld.edu.au/forms/test-show-hide-with-checkboxes/_nocache

 

So for the first question, 'What is your name?' I've ticked the 'Add a condition to this validation rule?'

and Validate the answer of 'Select a section' matches a regex of 0

 

Is zero correct as it's not working? I'm assuming it uses the value of the checkbox.

 

Thanks for your assistance.


(Nic Hubbard) #13

That link doesn't seem to be working. Just seeing keywords that have not been replaced.


(Joy) #14

Not sure what happened with that other form, try this one: https://www.qcaa.qld.edu.au/forms/validation-test

I've only added the 'Add a condition to this validation rule' to the Section 1 question


(Nic Hubbard) #15

If you are wanting the last two text fields required if you select checkbox 2, then you need to make sure you setup validation rules on those two text fields. Have you done that?


(Joy) #16

The validation on Section 1 isn't working, I haven't set up Section 2 or 3 yet as I was trying to get Section 1 working first