1
votes

I have two input fields for the users to input a range of year. If the user didn't enter anything, the page will run a default setting. If the user enter one of the field, he is required to enter the other field as well to get pass the validation.

Also I saved the rules as an array and put into application/config/form_validation.php. How do I do this?

2

2 Answers

0
votes

You would need to do a custom check. See the information about CallBacks on the CI Form Validation page. So you would set the call bak for field one. In the called back function you would implement your rules and return back a TRUE or FALSE.

0
votes

Just put your form validation inside of an if strlen($_POST['field_1'])>0 or if isset($_POST['field_1']) statement so that it only runs if there is a value within field_1.

if(isset($_POST['field_1'])){
     $this->form_validation->set_rules('field_1', 'Field 1', '');
     $this->form_validation->set_rules('field_2', 'Field 2', 'required');

    if (!$this->form_validation->run()) {
          $errors = validation_errors();
    } else {

        //do your thing

     }
}

Put this in your controller.