CodeIgniter Forums
Validation based on another field's value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Validation based on another field's value (/showthread.php?tid=39675)



Validation based on another field's value - El Forum - 03-17-2011

[eluser]Unknown[/eluser]
I'm fairly new to CodeIgniter, but I've been loving it so far. I'm almost done with my project, but I've run in to a problem. I've searched the forums and found topics that were similar, but they were either pre-2.0 or weren't quite what I wanted.

Basically I have a number of Yes/No radio buttons. If the user selects "Yes", I'd like to display an "Explain" input field that they will be required to fill out. If they choose "No" it is not a required field. I've got the display part working, but I'm having trouble figuring out to make "Explain" required based on the radio button value. This needs to happen for multiple radio buttons/explain fields.

Just a little background in to my set up, this is a multi-page form (only two pages need this functionality) and I'm using sessions to store the data until submitting. I have my Validation rules set up in a config file like so:

Code:
<?php
//config/form_validation.php
//Validation rules for application
$config = array(
    'step_1' => array(
       array(
             'field' => 'first_name',
             'label' => 'First Name',
             'rules' => 'required|trim|xss_clean'
          ),
       array(
             'field' => 'last_name',
             'label' => 'Last Name',
             'rules' => 'required|trim|xss_clean'
          ),
... etc.

Has anyone done anything similar before and gotten it to work? I was going to do a callback but I'm not 100% sure how I'd need to do it in this case.

Thank you in advance!


Validation based on another field's value - El Forum - 03-18-2011

[eluser]CroNiX[/eluser]
I haven't set up my rules using a multidimensional array like you are, but I usually do something like:
Code:
//the field with the checkbox
$check1 = $this->input->post('field_with_checkbox');
$rules = '';
if($check1 !== FALSE && $check1 == '1')
{
  //if the checkbox was checked, add a required field to the rules for the other field
  $rules .= 'required|';
}
//append regular rules for the field
$rules .= 'trim|xss_clean';
//apply rules to the textbox field, "Explain"
$this->form_validation->set_rules('field', 'label', $rules);

That might not be exactly right, I can't check right now.


Validation based on another field's value - El Forum - 03-21-2011

[eluser]Unknown[/eluser]
Hey CroNiX,

Thanks for the response! That seems to do the trick, but unfortunately it overwrites/ignores the other rules set in config/form_validation.php. It seems like I may to have to ditch the multidimensional array, which is a shame because I prefer to keep things organized that way. Unless anyone is aware of a way to add new rules to the config file through the controller?