且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Codeigniter表单验证

更新时间:2023-12-03 18:33:22

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

  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".