2

I'm trying to extend the validation class with my own function but it seems to skip the check.

My code

public function login_validation(){
 $this->load->library('form_validation');
 $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean|callback_validate_credentails');
 $this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
    if($this->form_validation->run()){
        #redirect('home');
        //Just to check 
        echo "login success";
    }
    else{
        $this->load->view('view_login');
    }
}

My validate credentails function goes like this;

public function validate_credentials(){
   //loads model_user
   $this->load->model('model_users');
   //checks if login_validate function in model_users return true
    if($this->model_users->login_validate()){
        return true;
    }
    else{
        //set validation message
        $this->form_validation->set_message('validate_credentails','Incorrect Username/Password');
        return false;
    }

}

My login_validate function in model_users if needed;

public function login_validate(){
    $this->db->where('email', $this->input->post('email'));
    $this->db->where('password', md5($this->input->post('password')));
    $query = $this->db->get('users');
    if($query->num_rows() == 1){
        return true;
    }
    else {
        return false; 
        }

}

This is the first time i've had this problem, am i doing something wrong? I've spent too much time on this :(

4
  • 3
    Check the file name is MY_form_validation.php and do you start the code like this "class MY_Form_validation extends CI_Form_validation {" inside the file? Linux system is case sensitive with file names check it as well. Commented Oct 24, 2012 at 12:47
  • You mean the form_validation file in systems/libraries? Commented Oct 24, 2012 at 12:49
  • Why does it need changing tho? Commented Oct 24, 2012 at 12:50
  • Actually you were right, i needed to change the name and add some little function. Thank you. Commented Oct 24, 2012 at 13:02

5 Answers 5

3

I just found the solution. I needed to change the name of the form_validation class as Nish suggested and add this function.

class MY_Form_validation extends CI_Form_validation {
    function run($module = '', $group = ''){
        is_object($module) AND $this->CI = &$module;
        return parent::run($group);
    }
} 
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me, I just wanted to add that the callback function should be a public function not private. I have wasted few minutes because of that.
2

Your callback is callback_validate_credentails (tails) and your function name is validate_credentials (tials). Typo maybe?

1 Comment

Sorry that was just a typo here, i spelt it correctly in my code.
1

I think the best advice is your LOGIN_validation check to be at your MODEL not at the form validation class... Its different THING !

1 Comment

I'm sending the form(in the view) to the function login_validation.
0

If you move the Callback_ function to the same controller it will work fine.

Comments

0

For Codeigniter 3 form validation callback Just add public $CI in MY_Form_validation

path: application/libraries/MY_Form_validation

class MY_Form_validation extends CI_Form_validation
{
    public $CI;
}

and then on your controllers constructor:

$this->load->library('form_validation');
$this->form_validation->CI =& $this;

It's a HMVC related callback problem. By this solution the current controller as the $CI variable to the form_validation library before assigning. This will allow your callback methods to function properly.

Comments

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.