Welcome Guest, Not a member yet? Register   Sign In
Help Needed. How to have a required field validation on a field that only posts if another field is a certain value


I have a form that unhides a field when a certain value is chosen in a select menu. If this value is chosen the field needs to be required.

However if I make the field required in the validation rules it won't submit the form if the value isn't chosen out of the select menu. How can I manipulate the validation rules so it will only set that field to be required if a certain value for another field is posted?

I tried this:
if($_POST['hearabout']=='The Investors Club') {
   $fields['supportMember']            = "required";
   $fields['supportMember']            = "callback_nothing";

But it doesn't set the field as 'Required' when the post value is set to 'The Investors Club'.

Any help would be greatly appreciated

try injecting a print_r($_POST) before that if statement to see what the $_POST['hearabout'] element contains. If you see it really has 'The Investors Club' within than I would suspect the way you are using that $fields array later on for setting validation rules.

Couldn't you do a callback on the hearabout field, and display an error if hearabout = The Investors Club and supportMember is empty?

[eluser]Colin Williams[/eluser]
You are doing it the correct way, using a conditional, but I'm not sure where $fields['supportMember'] makes it into the set_rules() method of Form_validation.

Theme © iAndrew 2016 - Forum software by © MyBB