Codeigniter’s validation class is awesome, but I ran into a situation where I wanted to set a rule that said something like:
Either Field A or Field B needs to be filled out, but not neither, and not both.
To do this I used PHP and popped a custom error on the form_falidation error array.
// Codeigniter doesn’t have a rule to say “this field or that field”
// So I’m making my own here, but passing the results back to codeigniter
// So it can display the errors uniformily to the view.
if( ($this->input->post(‘usergroups’) || $this->input->post(‘systemgroups’) )
&& !($this->input->post(‘usergroups’) && $this->input->post(‘systemgroups’)))
//Could do something here
$error=’A group must be selected. Only one group can be selected at a time.’;
$this->form_validation->_error_array['custom_error'] = $error;