Conditional Form Fields

hdouglas

Member
Not quite the normal type of conditional fields, this is quite a specific requirement, I wonder if anyone can think of a way of achieving this with Fabrik:-

I have a requirement where there will be about 50 fields on a form, but depending on the user who is to complete the form, will vary the fields required to be shown. This is going to vary on a user by user basis. The scenario is that currently each user completes a large multipage fabrik form (about 200 fields). Much of this form is conditional in that the questions offered are based on earlier responses. (A standard conditional method). The problem is that in this scenario fields cannot be madatory if they are only displayed conditionally. So for example:
What is your Employment Status? might open 1 of 2 groups depending on 'Employed' or 'Retired'
If Employed is checked, then the group of fields 'Employer' needs to be madatory (but I haven't found a way to achieve that).

So the solution is when the original form is processed, a list of 'missing' fields is created. If there are missing fields, the user is then directed to a new form where all of these missing fields are mandatory.

My question is can anyone recommend a way of building this second form based on the user record where the form comprises only the required fields?

Alternatively, is there a way to mandate a field in the original form which is hidden / visible depending on a previous answer?

thanks
Hugh
 
As a pro member you better post in the Professional Support forum to get the support you have paid for. (I'll move your thread).
You can show/hide elements and groups depending on an element value via element JS (show/hide a build in functions so you don't need to code any JS yourself).
If you have validations you can add a condition. Return false if the validation should be skipped.
 
As Troester says, making element validation mandatory depending on the status of other elements can be done with conditionals on the validation rules. Which requires a bit of fiddly coding, but works. All of the form's data is in the $formModel->formData[] array, which you can test in the "Condition" code for each validation.

So if field2 should only be validated if field1 has the value "1", put this in the Condition box for any validations on field2 ...

Code:
return $formModel->formData['yourtable___field1'] == '1';

So if field1 is not "1", this will return false, and the validation won't be run. Substitute your full element name for yourtable___field1.

Note that you may have to append _raw to the element name, if it's an element that has the concept of "value" and "label".

On my list of things to do "one day" is to automate this process ... so tie in validation to the element FX, with a "Don't validate if hidden" option for the form, so if an element has been hidden by virtue of the FX on another element, validations won't be run. But for now, you have to do it by hand with the Condition on each validation you need to be conditional.

-- hugh
 
Many thanks for this, I am having a few issues getting the PHP to recognise the value of the earlier selection field though.

I have setup a test form with just 3 fields, a dropdown and 2 fields. Each field is in its own group.

These are:
testvalid___select_group - Dropdown with 0:please select, 1:Group A, 2:Group B
testvalid___groupafield1 - Text field
testvalid___groupbfield1 - Text field

The logic I want to enforce is to validate testvalid___groupafield1 such that it is not empty, IF testvalid___select_group == 1 (Group A)

I have used a validation type 'notempty' and inserted
Code:
return $formModel->formData['testvalid___select_group_raw'] == '1';
into the Condition field on element testvalid___groupafield1
This has no effect. If I replace it with
Code:
return TRUE;
everything works
Have also tried removing the _raw but it has no effect
And tried replacing '1' with 'Group A' but again nothing.

I don't seem to be able to add a var_dump($formModel) or the like to the validation so I can't see what is being passed.

Is there something I am doing wrong?
 
Last edited:
Hmm, it seems $formModel is not defined in the validation condition. Hugh?
But you can follow the tooltip and use placeholders
return '{testvalid___select_group_raw}' == '1';

For debugging:
var_dump(...);exit;
should show you the output
 
Brilliant, thank you that works. For some reason I can't get the var_dump(); exit; to work, but that is not needed now.
thanks again
Hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top