How to create dependency validation between fields in CodeIgniter?

0

In CodeIgniter there is the possibility of creating validation rules ( rules ) for each form field, but what I need is a validation between two fields. I explain:

The form has a field called url which is only required if the corresponding flag is selected.

    
asked by anonymous 10.02.2014 / 18:22

2 answers

2

It is possible, but you will have to use the callback_XXX validators:

public function index(){
  $this->form_validation->set_rules('flag', 'Flag', '');
  $this->form_validation->set_rules('url', 'URL', 'callback_checaflag');
}

public function checaflag($url){
    if($this->input->post('flag')){
      return $this->form_validation->required('url');
    }
}
    
10.02.2014 / 20:15
0

Another alternative @LuisComS, would be to do something like:

public function index(){
    // Se tal campo estiver marcado, adicionar a regra para validar a URL
    if($this->input->post('eu_aceito')){
         $this->form_validation->set_rules('url', 'URL', 'callback_validaurl');
    }
}

public function validaurl($url){
    // Colocar aqui funcao para vallidar $url e retornar TRUE ou FALSE
}
    
10.02.2014 / 23:55