is_unique for codeigniter form validation

前端 未结 9 1103
走了就别回头了
走了就别回头了 2020-12-03 07:19

I\'m trying to figure out how I can use the is_unique rule from the Codeigniter form validation library in the following situation.

I\'m trying to submi

9条回答
  •  孤街浪徒
    2020-12-03 07:57

    Using your code as an example, the is_unique validation rule works by looking for a field called user_name in your users database table. If the field with the same value exists it validates as false.

    To make sure it runs only when the user submits a new value, you could check the posted value $this->input->post('user_name') against the value you pulled from the database to populate your form with. If they are the same, don't validate is_unique;

    if($this->input->post('user_name') != $original_value) {
       $is_unique =  '|is_unique[users.user_name]'
    } else {
       $is_unique =  ''
    }
    
    $this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean'.$is_unique);
    

提交回复
热议问题