【问题标题】:Codeigniter & HMVC Callback shows firstCodeigniter 和 HMVC 回调首先显示
【发布时间】:2017-09-09 02:34:15
【问题描述】:

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

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

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

问题如何确保它显示正确的验证消息 首先。

自动加载 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;
        }
    }
}

应用程序 > 库 > MY_Form_validation.php

<?php

class MY_Form_validation extends CI_Form_validation {

    public $CI;

} 

【问题讨论】:

    标签: codeigniter codeigniter-3 hmvc codeigniter-hmvc


    【解决方案1】:
    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]'); 
    }
    

    【讨论】:

    • 这是什么原因
    • 我还是有同样的问题
    • 你可以做一件事先检查post值是否为空? if(!empty($data['password'])) { $this->form_validation->set_rules('password', 'password', 'callback_validatePassword[password]'); }
    【解决方案2】:

    而不是这个 -

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

    请像这样使用回调函数 -

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-09
      • 2011-01-14
      • 2011-10-10
      • 1970-01-01
      • 2017-05-15
      • 2013-06-08
      • 1970-01-01
      • 2021-01-12
      相关资源
      最近更新 更多