8

I'm building a blog with CodeIgniter 3.1.3 and I have a feature in the admin to add new users. When I try to add a new user I get the following error:

The Password field does not match the confirm_password field.

although the given password in password and confrom password field is the same.

I can't figure out what causes the problem.

Here is my model:

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

Here is my controller

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');
        }

Here is my view:

<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 -->
2
  • Try this: $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[confirm_password]'); just in case it's a whitespace thing somewhre
    – bos570
    Jan 13, 2017 at 17:51
  • Unfotunately it didn't help.
    – PapT
    Jan 13, 2017 at 17:53

5 Answers 5

36
$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]');

Change your form validation to this and remember to autoload form validation in config/autoload.php file like $autoload['libraries'] = array('form_validation');. If it doen't work please comment below.

1
  • This worked for me - thank you. The key change was including the matches rule in the SECOND rule listed, rather than in the first. Although I had set form validation rules for both the password and confirm_password fields (unlike the OP, who did not have a rule for confirm_password), I had put the matches rule in the password field. This used to work in CI 2, but after upgrading to CI 3 it was throwing that "The Password field does not match" error. Thanks for the idea of how to fix it.
    – Dr Marble
    Jun 14, 2018 at 22:11
2

Here is the clean and simple way to achieve that.

Here is my controller code:

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');    
    }     
}

Now for Form validation:

  1. Create a form_validation.php file in config folder.

  2. Then inside autoload.php file write this line.

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

Here is my code of form_validation.php file.

<?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]'
                                    ]
                            ]
];

?>
1

config/autoload.php:

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

View:

<?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')); ?>

Controller:

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

You must use set rules two times for the double password Confirmation

$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

0

Create the form validation file in

application/config/form_validation.php

write the below code

<?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]'
    ]
]];

Controller code

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);
}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.