Codeigniter表单验证 [英] Codeigniter form validation

查看:80
本文介绍了Codeigniter表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在CodeIgniter的多个表单字段中显示单个消息吗?
例如,我为电子邮件和密码字段设置了以下规则。如果这两个字段中的任何一个无效,我想只显示一个消息。 (例如无效的电子邮件或密码)

  $ this-> form_validation-> set_rules('email_address'地址','valid_email | required'); 
$ this-> form_validation-> set_rules('password','Password','required | min_length [4]');



我该如何做?感谢任何帮助。



对不起,如果我的问题不清楚,目前我使用 validation_errors(),我得到每个字段的错误,但是,我想显示相同的错误消息,如果有(电子邮件或密码)无效,无论电子邮件是否无效,或密码无效,或两者都无效,则应打印单个邮件,例如:无效的电子邮件或密码。

解决方案

我不知道这是你需要的,但你可以尝试:

  if($ this-> form_validation-> run()== FALSE){
$ message ='您的错误讯息here'; // validation_errors
} else {
$ message ='您的成功消息在这里';
}

$ this-> load-> view('yourview',array('feedback_message'=> $ message));

如果你不在乎哪个字段无效,那么这个代码段是确定的。 有什么问题,我不在乎什么问题,告诉用户。


Can I display a single message for the multiple form fields in CodeIgniter? For example, I have set following rules for the email and password fields. I want to display only one message if any of these two fields is invalid. (eg. invalid email or password ")

$this->form_validation->set_rules('email_address', 'Email Address', 'valid_email|required');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');

How i can do that? Thanks for any help.

Edit: Sorry if my question is not clear. Currently I'm using validation_errors(), and i get errors of each field. However, I want to show a same error message if any of the two fields (email or password) is invalid. No matter if email is invalid, or if password is invalid, or both are invalid, it should print a single message, such as: invalid email or password.

解决方案

I'm not sure if this is what you need, but you can try:

if($this->form_validation->run() == FALSE){
   $message = 'Your error message here'; //validation_errors() works too.
}else{
   $message = 'Your success message here';
}

$this->load->view('yourview',array('feedback_message'=>$message));

If you don't care which field isn't valid, then this snippet is ok. "Something is wrong, i don't care what's wrong, tell the user".

这篇关于Codeigniter表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆