simple validation

prophoto

Active Member
I have a simple validation issue. I am using the notempty validation plugin, at least that seems to make sense.

Two fields, first is a drop down with values of 0-4. It has validation requiring greater than 0.

When the user chooses value #4, an upload element is shown. That upload element is required, but ONLY if the first drop down has a value of 4. By the tooltip I should be using this code, is that accurate?

Code:
return '{listname___dropdownelement}' == 4;
 
The condition looks correct to me.

Perhaps its an issue with the not empty validation on the fileupload element, could you post a screen shot of the fileupload's settings (or let me know which site / form this is on)

thanks
Rob
 
OK, I think this one is fixed, just needed to add _raw to the placeholder name, as it's a dropdown value, not label.

Remember that when dealing with elements that have a concept of "value" and "label", like dropdowns, radios, joins, etc., using just the element name as a placeholder, like {mytable___mydropdown}, will get you the "label" (say, "Photo Ad"). If you need the "value" (say, "4"), you need to append _raw, like {mytable___mydropdown_raw}.

Also, we usually recommend specifically "casting" to an integer type when you want to deal with whole numbers. This avoids the potential for PHP errors if your element value is empty. So you might want to change it to ...

PHP:
return (int)'{listname_dropdownelement_raw}' === 4;

This protects you against several potential situations that could result in an error, rather than a valid PHP fragment.

And yes, that triple === is deliberate.

If you were dealing with a decimal number, like 3.14, you would use (real) rather than (int).

-- hugh
 
It's not strictly necessary in this case, but === is "type safe", and will only return true if both values are the same value AND the same type.

In PHP, == is a "loose" comparison. So comparing an integer 4 with a string "4" will return true. Using === would return false if you compared integer 4 with string "4". It's typically safer to make sure that both values being compared are of the same type, as it can avoid unexpected results of "loose" comparisons, like how an empty string is treated, etc.

So in this case, we're "casting" your placeholder value to an integer, with the (int), and then doing a type safe (===) comparison with an integer (4, with no quotes round it). That way, we are absolutely sure we are getting what we expect.

Here's a good Stack Overflow Q&A:

http://stackoverflow.com/questions/...double-equals-and-identity-triple-equals-comp

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top