0

I'm having a problem with my validation. I'm using Codeigniter's Native Form Validation.

I'm doing a user update feature but, I fail to validate the oldpassword. Whenever I type correct value of oldpassword, it fails to submit. But when I type it incorrectly, it went good.

Well, this is a part of the controller

   $this->form_validation->set_rules('oldpassword', 'Old Password', 'required|callback_password_check');
   $this->form_validation->set_rules('password', 'New Password', 'required|matches[confirmpassword]');
   $this->form_validation->set_rules('confirmpassword', 'New Password Confirmation', 'required');

and this is the password checker function

function password_check($str){

    $result = $this->create_model->check_password($str);
     
       if($result)
       {         
         $this->form_validation->set_message('password_check', 'Password does not match!');
         return false;
       }
    else{
        
        return true;
    }       
}

and this is the model of the checker

function check_password($str){

         $this->db->select('*');
         $this->db->from('users');
         $this->db->where('id', $this->input->post('id'));          
         $this->db->limit(1);

         $query = $this->db-> get();

         if($query->num_rows() == 1)   {

             //GETS PASSWORD FOR HASHING
             foreach($query->result() as $rows)    {
                $hash = $rows->password;
             }         
             //VERIFY PASSWORD
             if (password_verify($str, $hash)) {
                   $this->session->set_flashdata('success', 'Success! You have changed the password');    //DONT MIND THIS, ITS A TEST          
                   return true;
              } else {
                  $this->session->set_flashdata('error', 'Old Password is incorrect'); //DONT MIND THIS, ITS A TEST 
                  return false;
              }

          } else   {

           return false;

         }
}

1 Answer 1

0

You condition is incorrect in function password_check($str). Replace

if($result)

to

if(!$result)
Sign up to request clarification or add additional context in comments.

1 Comment

So you think this page should be closed as Off-topic Typo?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.