This is a quick tip if your’re looking for a “required_if” rule in CodeIgniter. Basically the rule is as follows: Make form field-A required if form field-B is filled in. The most common case scenario is changing a password, where the original password is required first, but is conditional only if a new password is entered.

If your form validation rules are defined on runtime you can simply modify your rules during processing as such:
$required_if = $this->input->post('password') ? '|required' : '' ; $this->form_validation->set_rules('current_password', 'current password', 'trim'. $required_if .'|min_length[6]'); $this->form_validation->set_rules('password', 'password', 'trim|min_length[6]|matches[password_confirm]'); $this->form_validation->set_rules('password_confirm', 'password confirmation', 'trim');
{ 2 comments… read them below or add one }
heya! thanks for this. worked like a charm.
Good tip, works great. Thanks.