How to do a negative form validation in codeigniter?

前端 未结 3 1674
走了就别回头了
走了就别回头了 2021-01-18 09:27

is_unique is the form validation which not allow the value exist in the database. But, can I do the opposite one? for example, I would like to need the value w

3条回答
  •  时光取名叫无心
    2021-01-18 09:39

    You can do it like this

    $this->form_validation->set_rules('email', 'Email', 'required|max_length[32]|valid_email|callback_email_available');
    

    Controller method

    public function email_available($str)
    {
        // You can access $_POST variable
        $this->load->model('mymodel');
        $result =   $this->mymodel->emailAvailability($_POST['email']);
        if ($result)
        {
            $this->form_validation->set_message('email_available', 'The %s already exists');
            return FALSE;
        }else{
            return TRUE;
        }
    }
    

    Model mymodel.php

    public function emailAvailability($email)
    {
        $this->db->where('email',$email);
        $query  =   $this->db->get('tablename');
        return $query->row();
    }
    

提交回复
热议问题