Codeigniter匹配返回false

时间:2019-02-08 16:03:19

标签: php codeigniter

我试图将帐户保存到数据库。 当使用form_validation验证表单时,即使我尝试插入正确的值,它也总是返回false。这是代码

public function save(){
            $params = $this->input->post();

            if(!empty($params['id_account']) && !empty($params['password']) ){

                    $this->form_validation->set_rules('confPass', 'Confirmation Password', 'matches[password]');

            }else{
                $this->form_validation->set_rules('password', 'Password', 'required');
                $this->form_validation->set_rules('confPass', 'Confirmation Password', 'required|matches[password]');
            }


            $this->form_validation->set_data($params);
            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('id_role', 'Role', 'required');

            if ($this->form_validation->run() == TRUE) {
                unset($params['confPass']);
                $params['account_status'] = array_key_exists('account_status', $params) ? $params['account_status'] : ACCOUNT_STATUS_PENDING;  
                $this->load->model('user_model');
                $this->user_model->save($params);
                $this->session->set_flashdata('notif', '<div class="alert alert-success">Berhasil menyimpan data.</div>');
                redirect('rbac/form/account');
            } else {
                $error = validation_errors();

                echo '<pre>';
                print_r($error);
                echo '</pre>';

                var_dump($params['password']);
                var_dump($params['confPass']);
                die();

                // $this->session->set_flashdata('notif', '<div class="alert alert-danger">'.$error.'</div>');
                // redirect('rbac/form/account');
            }
        }

我尝试返回validation_errors()$params['confPass']$params['password'],结果如下:

  

“确认密码”字段与密码字段不匹配。

     

string(1)“ e”字符串(1)“ e”

您会看到$params['confPass']$params['password']是匹配的。

1 个答案:

答案 0 :(得分:0)

if(!empty($params['id_account']) && !empty($params['password']) ){
                $this->form_validation->set_rules('confPass', 'Confirmation Password', 'matches[password]');
        }else{
            $this->form_validation->set_rules('password', 'Password', 'required');
            $this->form_validation->set_rules('confPass', 'Confirmation Password', 'required|matches[password]');
        }

当您在上面编写的条件为true时,即控件位于if(下方)块中

if(!empty($params['id_account']) && !empty($params['password']) )

您没有为“密码”字段设置条件,仅具有确认密码规则。什么都不匹配,尽管密码匹配也总是返回false。

您需要添加密码验证规则

 $this->form_validation->set_rules('password', 'Password', 'required');

在确认要匹配密码的密码规则之前。

如果您的情况表明它不需要密码,则可以执行类似的操作

 $this->form_validation->set_rules('password', 'Password', 'min_length[5]');

满足条件但不是“必需”的任何内容,即修剪或最小长度

希望有帮助。

相关问题