php Codeigniter 表单验证错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8429429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter form validation error message
提问by stergosz
I have a form on my website header where i allow the user to log in with his username/password... then i POST to /signin page and check if the username exists to allow the user to log in.. if there is a problem upon login i output these errors...
我的网站标题上有一个表单,我允许用户使用他的用户名/密码登录......然后我发布到 /signin 页面并检查用户名是否存在以允许用户登录......如果有登录时出现问题我输出这些错误...
i tried using the following code to show a custom error but with no luck
我尝试使用以下代码来显示自定义错误,但没有运气
if ($this->form_validation->run() == false){
$this->load->view("login/index", $data);
}else{
$return = $this->_submitLogin();
if ($return == true){
//success
}else{
$this->form_validation->set_message('new_error', 'error goes here');
//error
}
$this->load->view("login/index", $data);
}
how does set_message work and if this is the wrong method, which one allow me to show a custom error in this case?
set_message 是如何工作的,如果这是错误的方法,在这种情况下,哪种方法允许我显示自定义错误?
EDIT :
编辑 :
validation rules:
验证规则:
private $validation_rules = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'trim|required|callback__check_valid_username|min_length[6]|max_length[20]|xss_clean'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|min_length[6]|max_length[32]'
),
);
回答by toopay
The set_message
method allows you to set your own error messages on the fly. But one thing you should notice is that the key name has to match the function name that it corresponds to.
该set_message
方法允许您即时设置自己的错误消息。但是您应该注意的一件事是键名必须与其对应的函数名相匹配。
If you need to modify your custom rule, which is _check_valid_username
, you can do so by perform set_message
within this function:
如果您需要修改您的自定义规则,即_check_valid_username
,您可以通过set_message
在此函数中执行来实现:
function _check_valid_username($str)
{
// Your validation code
// ...
// Put this in condition where you want to return FALSE
$this->form_validation->set_message('_check_valid_username', 'Error Message');
//
}
If you want to change the default error message for a specific rule, you can do so by invoking set_message
with the first parameter as the rule name and the second parameter as your custom error. E.g., if you want to change the required
error :
如果要更改特定规则的默认错误消息,可以通过set_message
将第一个参数作为规则名称和第二个参数作为自定义错误进行调用来实现。例如,如果您想更改required
错误:
$this->form_validation->set_message('required', 'Oops this %s is required');
If by any chance you need to change the language instead of the error statement itself, create your own form_validation_lang.php
and put it into the proper language folder inside your system language directory.
如果您需要更改语言而不是错误语句本身,请创建自己form_validation_lang.php
的语言并将其放入系统语言目录中的正确语言文件夹中。
回答by StathisG
As you can see here, you can display the custom error in your view in the following way:
正如您在此处看到的,您可以通过以下方式在您的视图中显示自定义错误:
<?php echo form_error('new_error'); ?>
PS: If this isn't your problem, post your corresponding view code and any other error message that you're getting.
PS:如果这不是您的问题,请发布相应的视图代码和您收到的任何其他错误消息。
回答by Vasil Dakov
The problem is that your form is already validated in your IF part! You can fix the problem by this way:
问题是您的表单已经在您的 IF 部分进行了验证!您可以通过以下方式解决问题:
if ($this->form_validation->run() == false){
$this->load->view("login/index", $data);
}else{
$return = $this->_submitLogin();
if ($return == true){
//success
}else{
$data['error'] = 'Your error message here';
//error
}
$this->load->view("login/index", $data);
}
In the view:
在视图中:
echo $error;
The CI way to check user credentials is to use callbacks:
检查用户凭据的 CI 方法是使用回调:
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
...
public function username_check($str) {
// your code here
}
I recommend you to read CI documentation: http://codeigniter.com/user_guide/libraries/form_validation.html
我建议您阅读 CI 文档:http: //codeigniter.com/user_guide/libraries/form_validation.html
回答by Kinjal Dixit
The way I did this was to add another validation rule and run the validation again. That way, I could keep the validation error display in the view consistent.
我这样做的方法是添加另一个验证规则并再次运行验证。这样,我可以使视图中的验证错误显示保持一致。
The following code is an edited excerpt from my working code.
以下代码是我的工作代码的编辑摘录。
public function login() {
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$data['content'] = 'login';
if($this->form_validation->run()) {
$sql = "select * from users where email = ? and password = ?";
$query = $this->db->query($sql, array($this->input->post('email'), $this->input->post('password')));
if($query->num_rows()==0) {
// user not found
$this->form_validation->set_rules('account', 'Account', 'callback__noaccount');
$this->form_validation->run();
$this->load->view('template', $data);
} else {
$this->session->set_userdata('userid', $query->id);
redirect('/home');
}
} else {
$this->load->view('template', $data);
}
}
public function _noaccount() {
$this->form_validation->set_message('_noaccount', 'Account must exist');
return FALSE;
}
回答by Limitless isa
Require Codeigniter 3.0
需要 Codeigniter 3.0
Using callback_ method;
使用 callback_ 方法;
class My_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->form_validation->set_message('date_control', '%s Date Special Error');
}
public function date_control($val, $field) { // for special validate
if (preg_match("/^[0-9]{2}.[0-9]{2}.[0-9]{4}$/", $val)) {
return true;
} else {
return false;
}
}
public function my_controller_test() {
if ($this->input->post()) {
$this->form_validation->set_rules('date_field', 'Date Field', 'trim|callback_date_control[date_field]|xss_clean');
if ($this->form_validation->run() == FALSE) {
$data['errors']=validation_errors();
$this->load->view('my_view',$data);
}
}
}
}
Result:
结果:
if date = '14.07.2017' no error
如果日期 = '14.07.2017'没有错误
if date = '14-7-2017' Date Field Date Special Error
if date = '14-7-2017'日期字段日期特殊错误