CodeIgniter Forums
Help Needed. How to have a required field validation on a field that only posts if another field is a certain value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Help Needed. How to have a required field validation on a field that only posts if another field is a certain value (/thread-23570.html)



Help Needed. How to have a required field validation on a field that only posts if another field is a certain value - El Forum - 10-14-2009

[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


Help Needed. How to have a required field validation on a field that only posts if another field is a certain value - El Forum - 10-14-2009

[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.


Help Needed. How to have a required field validation on a field that only posts if another field is a certain value - El Forum - 10-14-2009

[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?


Help Needed. How to have a required field validation on a field that only posts if another field is a certain value - El Forum - 10-15-2009

[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.