无法获得 form_validation->set_rules 和 form_validation->set_message 错误
unable to get form_validation->set_rules and form_validation->set_message error
我在 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
在要查看错误消息的视图文件 (template.php) 中使用 echo validation_errors()
我在 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
在要查看错误消息的视图文件 (template.php) 中使用 echo validation_errors()