Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirm password in CodeIgniter

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 -->
like image 623
PapT Avatar asked Jan 13 '17 17:01

PapT


1 Answers

$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.

like image 54
Geordy James Avatar answered Nov 15 '22 21:11

Geordy James