[solved] How to add validation condition if ... ?

germanian

Member
I have two elements on form:

Element 1: Checkbox with the label "Do you want tell me your age?"
Element 2: Text field with the label "Please type in your age now"

I've set a JavaScript, that element 2 is only visible, if element 1 is checked.
I've set a validation, that element 2 should not be empty.

But if element 1 os not checked, and user is trying to save the form data, then the valisation says element 2 must not be empty.

How can i set a condition to the validation, that element 2 should not be empty, if element 2 is checked only. If element 1 is not checked, then this validation should be ignored.

Could someone give me an example?
 

Attachments

  • temp1.jpg
    temp1.jpg
    70.9 KB · Views: 830
Hi germanian,
Maybe you should use a different validation?
If you use the isgreaterthanlessthan validation, you can force a range of ages, say 18 to 100 and prevent ridiculous entries. This validation also has an allow empty option.
As for the user checking the box to see element 2, then entering a value - this can still be a problem.
If it were me, I would add a little php script in the Forms->Plug-ins with a little code that does:
Code:
//Test if element 1 is checked
If ({tablename___element1}=false)
//Test if element 2 is not empty
{If (!empty({tablename___element2})
//If element 2 is not empty, change it to empty
{$formModel->updateFormData('tablename___element2','', true);}
}
There are probably more elaborate ways to do this with javascript, but if your user leaves element 1 unchecked this just clears any value they've left in element 2. If they've checked element 1, the validation will force them to enter values within the range you've set.

-J
 
Thank you for your help.

I failed at first example already.

I've tried:
If ({personaldata___doyouwant}=false)
If (personaldata___doyouwant=false)

Do i need to add something like - then return 'true' - or - a ; at the end of line?

I've tried to find some information about it on wiki or in forum, but i also failed :c)
 
The =false part needs to match whatever value you've set for the value in checkbox element. Also, because the way Fabrik lets you choose the value, you may need to write it as ='false'.

Generally, I would set the values to a number, so I would set the default value=0 and the checked value=1, then in the if statement use =0 instead of =false. I wrote false for readability before.

You can get good examples of how to use the php if statement from w3schools.com or tizag.com - they usually keep it clean and simple.

-J
 
Still no luck with:
If (personaldata___doyouwant=0)

At w3schools.com i can find a lot of different languages to learn, which one is suitable for the validation condition field?
 
In the settings for the checkbox element you need to enter the value that will record the state in the database.

The code will try to match this value.

You need to learn some php.
 
Ahhhh, php belongs into the validation condition field? I presumed this already in post #4 :c)
I know some php and i know that ; belongs to the end of every line.
And i know that an if command should compare with == and not with =
And i know that after ( ) should be a command that willl be executed if anything between ( ) is valid, like 'return true' or 'return false'.

So, then, first example in post#2 seems to be totally wrong, sorry mygunfriday.

this one works:

if ({personaldata___doyouwant}==true) return true;
 
I think you missed where I wrote "If it were me, I would add a little php script in the Forms->Plug-ins".
And yes, you need the double ==. I missed that. You've been putti g the code in the wrong place. My code goes in as a Form->Plugin, not in the condition.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top