Codeigniter& HMVC Callback首先显示

时间:2017-04-13 05:39:01

标签: codeigniter codeigniter-3 hmvc codeigniter-hmvc

在我的codeigniter版本3.1.4 / hmvc登录上,如果表单验证密码输入因某种原因为空,则回调函数首先显示而不是显示所需的消息?

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

如消息所示,它应首先显示密码所需的消息而不是回调

enter image description here

  

问题如何确保显示正确的验证消息   第一

自动加载的form_validation

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

控制器

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

应用程序&gt;库&gt; MY_Form_validation.php

<?php

class MY_Form_validation extends CI_Form_validation {

    public $CI;

} 

2 个答案:

答案 0 :(得分:1)

代替这个-

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

请像这样使用回调函数-

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

答案 1 :(得分:0)

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