Codeigniter & HMVC Callback shows first

430 Views Asked by At

On my codeigniter Version 3.1.4 / hmvc login if the form validation password input is empty for some reason the callback function shows first rather than the required message?

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

As shown on message it should show the password required message first rather than callback

enter image description here

Question how can I make sure it shows the correct validation messages first.

Autoloaded form_validation

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

Controller

<?php

class Login extends MX_Controller {

    public $CI;

    public function __construct() {
        parent::__construct();
        $this->load->model('catalog/user/user_model');

        $this->form_validation->CI =& $this;
    }

    public function index() {
        $data['type'] = $this->input->post('type');
        $data['password'] = $this->input->post('password');

        $this->form_validation->set_rules('type', 'Username Or Email', 'trim|required');
        $this->form_validation->set_rules('password', 'password', 'required|callback_validatePassword[password]');

        if ($this->form_validation->run() == false) {

            $data['header'] = Modules::run('catalog/common/header/index');
            $data['footer'] = Modules::run('catalog/common/footer/index');

            $this->load->view('template/account/login', $data);

        } else {

            $user_info = $this->user_model->get_user($user_id = '', $this->input->post('type', true));

            if ($user_info) {

                $remember = $this->input->post('remember') ? TRUE : FALSE;

                $this->user->login($user_info['user_id'], $remember);

                redirect(base_url("users") .'/'. $user_info['user_id'] .'/'. $user_info['username']);
            }
        }
    }

    public function validatePassword($str) {
        if ($this->user_model->validate_password($str, $this->input->post('type',  true))) {
            return TRUE;
        } else {
            $this->form_validation->set_message('validatePassword', 'Opp\'s somthing wrong with login!');
            return FALSE;
        }
    }
}

application > libraries > MY_Form_validation.php

<?php

class MY_Form_validation extends CI_Form_validation {

    public $CI;

} 
2

There are 2 best solutions below

3
438sunil On
just keep the callback validation separate
$this->form_validation->set_rules('password', 'password', 'trim|required');
// check for post values is empty or not?
if(!empty($data['password'])) {
    $this->form_validation->set_rules('password', 'password', 'callback_validatePassword[password]'); 
}
0
Piku On

Instead of this -

$this->form_validation->set_rules('password', 'password', 'required|callback_validatePassword[password]');

kindly use the callback function like this -

$this->form_validation->set_rules('password', 'password', 'required', 'callback_validatePassword');