在CodeIgniter中确认密码

时间:2017-01-13 17:43:04

标签: php codeigniter

我正在使用CodeIgniter 3.1.3构建一个博客,我在管理员中有一项功能可以添加新用户。当我尝试添加新用户时,出现以下错误:

  

密码字段与confirm_password字段不匹配。

虽然密码和confrom密码字段中的给定密码是相同的。

我无法弄清楚导致问题的原因。

这是我的模特:

public function insert($data){
    $this->db->insert('users', $data);
    return true;
}

这是我的控制器

public function add(){
        //Validation Rules
        $this->form_validation->set_rules('first_name','First Name','trim|required');
        $this->form_validation->set_rules('last_name','Last Name','trim|required');
        $this->form_validation->set_rules('email','Email','trim|required|valid_email');
        $this->form_validation->set_rules('username','Username','trim|required|min_length[3]');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_password]');

        $data['groups'] = $this->User_model->get_groups();

        if($this->form_validation->run() == FALSE){
            //Views
            $data['main_content'] = 'admin/users/add';
            $this->load->view('admin/layouts/main', $data);
        } else {
            //Create Data Array
            $data = array(
                'first_name'    => $this->input->post('first_name'),
                'last_name'     => $this->input->post('last_name'),
                'username'      => $this->input->post('username'),
                'password'      => md5($this->input->post('password')),
                'group_id'      => $this->input->post('group'),
                'email'         => $this->input->post('email')
            );

            //Table Insert
            $this->User_model->insert($data);

            //Create Message
            $this->session->set_flashdata('user_saved', 'User has been saved');

            //Redirect to pages
            redirect('admin/users');
        }

以下是我的观点:

<div class="row">
                        <div class="col-lg-12">
                            <div class="form-group">
                                <label>First Name</label>
                                <input class="form-control" type="text" name="first_name" value="<?php echo set_value('first_name'); ?>" placeholder="Enter First Name" />
                            </div>
                            <div class="form-group">
                                <label>Last Name</label>
                                <input class="form-control" type="text" name="last_name" value="<?php echo set_value('last_name'); ?>" placeholder="Enter Last Name" />
                            </div>  
                            <div class="form-group">
                                <label>Email Address</label>
                                <input class="form-control" type="email" name="email" value="<?php echo set_value('email'); ?>" placeholder="Enter Email" />
                            </div>  
                            <div class="form-group">
                                <label>Username</label>
                                <input class="form-control" type="text" name="username" value="<?php echo set_value('username'); ?>" placeholder="Enter Username" />
                            </div>  
                            <div class="form-group">
                                <label>Password</label>
                                <input class="form-control" type="password" name="password" value="<?php echo set_value('password'); ?>" placeholder="Enter Password" />
                            </div>
                            <div class="form-group">
                                <label>Confirm Password</label>
                                <input class="form-control" type="password" name="confirm_password" value="<?php echo set_value('confirm_password'); ?>" placeholder="Confirm Password" />
                            </div>
                            <div class="form-group">
                                <label>User Group</label>
                                <select name="group" class="form-control">
                                     <?php foreach($groups as $group) : ?>
                                    <option value="<?php echo $group->id; ?>"><?php echo $group->name; ?></option>
                                 <?php endforeach; ?>   
                                </select>
                            </div>  

                    </div><!-- /.row -->

5 个答案:

答案 0 :(得分:19)

$this->form_validation->set_rules('first_name','First Name','trim|required');
$this->form_validation->set_rules('last_name','Last Name','trim|required');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('username','Username','trim|required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|matches[password]');

将表单验证更改为此并记住在config / autoload.php文件中自动加载表单验证,如$autoload['libraries'] = array('form_validation');。如果它不起作用,请在下面评论。

答案 1 :(得分:1)

这是实现这一目标的简洁明了的方法。

这是我的controller代码:

public function add_user(){
    $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');    
    if ($this->form_validation->run('register_user_rules') == FALSE){
        $this->load->template('user/register');     
    } else {
        $post = $this->input->post();
        unset($post['pass_confirm']);
        unset($post['submit']);
        if($this->UserModel->addUser($post)){
            $this->session->set_flashdata('feedback', 'Registration Succesfull! Please Sign in to Continue');
            $this->session->set_flashdata('feedback_class', 'alert-success');
        } else {
            $this->session->set_flashdata('feedback', 'Sorry! Please try again.');
            $this->session->set_flashdata('feedback_class', 'alert-danger');
        }
        return redirect('register');    
    }     
}

现在进行表单验证:

  1. form_validation.php文件夹中创建一个config文件。

  2. 然后在autoload.php文件中写入此行。

    $autoload['libraries'] = array('form_validation');

这是我的form_validation.php文件的代码。

<?php

$config = [
        'register_user_rules' => [
                                    [
                                        'field' => 'fullname',
                                        'label' => 'Full Name',
                                        'rules' => 'trim|required|max_length[20]'
                                    ],
                                    [
                                        'field' => 'email',
                                        'label' => 'Email Address',
                                        'rules' => 'trim|required|valid_email'

                                    ],
                                    [
                                        'field' => 'phone',
                                        'label' => 'Phone Number',
                                        'rules' => 'trim|required|numeric|max_length[10]|regex_match[/^[0-9]{10}$/]'

                                    ],  
                                    [
                                        'field' => 'password',
                                        'label' => 'Password',
                                        'rules' => 'trim|required'
                                    ],
                                    [
                                        'field' => 'pass_confirm',
                                        'label' => 'Confirm Password',
                                        'rules' => 'trim|required|matches[password]'
                                    ]
                            ]
];

?>

答案 2 :(得分:0)

config / autoload.php:

$autoload['libraries'] = array('form_validation');

查看:

<?php echo form_label('Password:'); ?>
<?php echo form_input(array('placeholder'=>'Enter your password', 'type'=>'password', 'name'=>'password')); ?>

<?php echo form_label('Confirm password:'); ?>
<?php echo form_input(array('placeholder'=>'Confirm your password', 'type'=>'password', 'name'=>'confirm_password')); ?>

控制器:

$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confirm_password', 'Confirm password', 'required|matches[password]');

答案 3 :(得分:0)

您必须两次使用设置规则进行两次密码确认

$this->form_validation->set_rules('username','Username','trim|required|min_length[3]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_password]'); 

http://prntscr.com/kl4mfu
http://prntscr.com/kl4m7u
http://prntscr.com/kl4lvm

答案 4 :(得分:0)

在其中创建表单验证文件

application / config / form_validation.php

编写以下代码

<?php $config =   [
'add_user'  =>  [

   [
        'field'     =>  'first_name',
        'label'     =>  'First Name',
        'rules'     =>  'trim|required'
    ],
    [
        'field'     =>  'last_name',
        'label'     =>  'Last Name',
        'rules'     =>  'trim|required'
    ],
    [
        'field'     =>  'email',
        'label'     =>  'Email Address',
        'rules'     =>  'required|valid_email'
    ],
    [
        'field'     =>  'username',
        'label'     =>  'Username',
        'rules'     =>  'trim|required|min_length[3]'
    ],
    [
        'field'     =>  'password',
        'label'     =>  'Password',
        'rules'     =>  'required|matches[confirm_password]'
    ]
]];

控制器代码

function add(){
$this->load->helper('form');

if($this->form_validation->run('add_user')){
    
    $post   =   $this->input->post();

    $_data  =   array(
        'first_name'       =>   $post['first_name'],
        'last_name'        =>   $post['last_name'],
        'username'         =>   $post['username'],
        'password'         =>   md5($post['password']);
        'group_id'         =>   $post['group'],
        'email'            =>   $post['email']  
    );

    // insert into table
    $_insert = $this->User_model->insert($_data);
    
    if($_insert){
        // if data is saved show the success message.
        $this->session->set_flashdata('user','User has been saved');
    } else {
        // if failed to save show the failed message.
        $this->session->set_flashdata('user','Failed to save the user data');
    }
    // Redirect to users page
    return redirect('admin/users');
} else {
    $data['main_content']   =   'admin/users/add';
    $this->load->view('admin/layouts/main',$data);
}}