【问题标题】:CodeIgniter - Unable to access an error message corresponding to your field nameCodeIgniter - 无法访问与您的字段名称对应的错误消息
【发布时间】:2017-08-24 11:44:00
【问题描述】:

我正在创建一个库来添加新用户。

class Register
    {
        private $CI;
        public function add_new_user()
        {
            $CI =& get_instance();
            $CI->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');
            if ($CI->form_validation->run() == TRUE)
            {
                $email = $_POST['email'];
                $insert_data = array('email' => $email);
                $CI->new_data->add_user($insert_data);
            }
        }
        private function is_email_exist($email)
        {
            $CI =& get_instance();
            $email_result = '';
            $query = $CI->check->find_email($email);
            foreach ($query->result_array() as $row)
            {
                $email_result = $row['email'];
            }
            if ($email_result == $email)
            {
                $CI->form_validation->set_message('is_email_exist', 'Such email already exist!');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
    }

我将 form_validation 和模型 check, new_data 添加到自动加载中。当我提交表单而不是错误(如果应该存在)时,我得到 无法访问与您的字段名称用户名相对应的错误消息。(is_username_exist)。我应该怎么做才能得到正确的错误?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    Codeigniter 表单验证是针对控制器的,所以当提交表单时它会去那里

    https://www.codeigniter.com/user_guide/libraries/form_validation.html#the-controller

    控制器方式

    <?php
    
    class Register extends CI_Controller
    {
    
        public function __construct() {
            parent::__construct();
            $this->load->library('form_validation');
            $this->load->model('some_model');
        }
    
        public function index() {
            $this->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');
    
            if ($this->form_validation->run()) {
    
                $insert_data = array(
                    'email' => $this->input->post('email')
                );
    
                $this->some_model->new_data->add_user($insert_data);
            }
    
            $data['title'] = 'Welcome To Codeigniter';
    
            $this->load->view('header', $data);
            $this->load->view('someview', $data);
            $this->load->view('footer');
    
        }
    
        // Check user email
        public function is_email_exist() {
            // Single input check
            $this->db->where('email', $this->input->post('email'));
            $query = $this->db->get('user');
    
            // If user email greater than 0
            if ($query->num_rows() > 0) {
                return TRUE;
            } else {
                $this->form_validation->set_message('is_email_exist', 'Such email already exist!');
                return FALSE;
            }
    
        }
    
    
    }
    

    【讨论】:

    • 你应该解释你做了什么。否则 OP 可能只是复制粘贴此内容并在下次遇到错误时发布新帖子。
    • 是的,但是如果我需要在许多控制器中使用此代码,我应该将此代码复制到所有控制器,或者我可以创建一个库并在我需要的任何地方使用$this-&gt;my_library-&gt;my_methodevry?
    【解决方案2】:

    将 'is_email_exist' 替换为 __FUNCTION__ 为我工作,

    https://stackoverflow.com/a/38950446/2420302

    【讨论】:

      猜你喜欢
      • 2012-05-23
      • 2022-01-05
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      相关资源
      最近更新 更多