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
#1

[eluser]Unknown[/eluser]
Hi

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:
Code:
if($_POST['hearabout']=='The Investors Club') {
   $fields['supportMember']            = "required";
}else{
   $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
#2

[eluser]bretticus[/eluser]
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.
#3

[eluser]kirkaracha[/eluser]
Couldn't you do a callback on the hearabout field, and display an error if hearabout = The Investors Club and supportMember is empty?
#4

[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