【问题标题】:unable to get form_validation->set_rules and form_validation->set_message error无法获取 form_validation->set_rules 和 form_validation->set_message 错误
【发布时间】:2015-06-18 09:05:29
【问题描述】:

我在 autoload.php 中有自动加载辅助表单和库表单验证

 <?php
  defined('BASEPATH') OR exit('No direct script access allowed');

 class Login extends CI_Controller {


    var $data;
    public function __construct(){
        parent::__construct();
        $this->data=array('page'=>'login_view','title'=>'login');
           $this->load->model("Login_model");

    }
  public function index()
 {
      //get the posted values
    $data=$this->data;
    $this->load->view('template',$data);

 }
 public function checklogin(){
    //form validation
        $data=$this->data;
        $this->form_validation->set_rules('username','Username','required');
        $this->form_validation->set_rules('password' ,'Password','required|callback_verifyUser');
            if($this->form_validation->run()==false){
            $this->load->view('template',$data);
        }
        else{

        }
 }
 public function verifyUser(){
    $name=$this->input->post('username');
    $pass=$this->input->post('password');
      $option=$this->input->post('optionlist');

    if($this->Login_model->login($name,$pass,$option)){
        return true;

    }else{
                    //user doesn't exist
           //echo "Wrong username and password";
         $this->form_validation->set_message('verifyUser','Incorrect officer_id or password. Please try again');
        return false;

    }

 }
}

?>

问题是它不显示任何错误消息 USERNAME IS REQUIRED OR PASSWORD IS REQUIRED WHEN I LEAVE IT BLANK 。它只是重定向到 LOGIN_VIEW.PHP 而不显示任何错误消息,即用户名留空

文档说表单验证用于显示错误。

对于视图 login_view.php,您可以在此处查看表单 http://pastebin.com/T2zMYGn1

【问题讨论】:

标签: php codeigniter codeigniter-2 codeigniter-3


【解决方案1】:

在要查看错误消息的视图文件 (template.php) 中使用 echo validation_errors()

【讨论】:

  • 已经在视图 login_view 中使用了 =validation_errors();?>
  • 如我所说,在template.php 文件中试试这个,它会起作用,因为你的错误在那里触发
  • 很好,你可以接受我的回答。但是,您的代码结构不佳,如果您愿意,我可以建议更好的方法。
  • 它有效,但由于放置在 template.php picpaste.com/doubt-MM4DoGlx.png,消息被放置在不同的位置
  • 您甚至可以在相应字段下方单独显示错误。看看codeigniter.com/user_guide/libraries/…
猜你喜欢
  • 1970-01-01
  • 2012-12-08
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 2016-09-22
  • 1970-01-01
相关资源
最近更新 更多