使用CodeIgniter表单验证的自定义错误消息

时间:2022-11-24 14:50:43

I want to make some custom error messages in my CodeIgniter forms. I've tried using

我想在我的CodeIgniter表单中制作一些自定义错误消息。我试过用过

$this->form_validation->set_message('is_unique[users.username]', 'The username is already taken');

$ this-> form_validation-> set_message('is_unique [users.username]','用户名已被占用');

However I can't get it working.

但是我无法让它发挥作用。

Editing the form_validation_lang.php file is not good enough, as is_unique will be The username is already taken for usernames, and The e-mail is already registered for mails.

编辑form_validation_lang.php文件是不够的,因为is_unique将是已经为用户名使用的用户名,并且已经为邮件注册了电子邮件。

How can I make this custom error message?

如何制作此自定义错误消息?

Here's a snippet from my code:

这是我代码中的一个片段:

$this->form_validation->set_message('is_unique[users.username]', 'The username is already taken');

// Check if username has changed
if ($this->input->post('username') !== $user->username) {
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[20]|is_unique[users.username]');
}

5 个解决方案

#1


20  

Right way of doing this is by passing a string format

正确的方法是传递字符串格式

$this->form_validation->set_message('is_unique', 'The %s is already taken');

So, then only we can able to get message like "This Username is already taken" or "This Email is already taken".

那么,只有我们能够获得诸如“此用户名已被采用”或“此电子邮件已被采用”之类的消息。

#2


2  

This worked for me

这对我有用

$this->form_validation->set_message('is_unique', 'The username is already taken');

#3


2  

It's better you use like this:

你最好这样使用:

$this->form_validation->set_message('is_unique', 'The %s is already taken');

The form_validation changes %s with the label seted for the field.

form_validation使用为该字段设置的标签更改%s。

Hope this helps!

希望这可以帮助!

#4


0  

you can add your custom message to validation_errors() string after checking validation using $this->form_validation->run() == true

您可以在使用$ this-> form_validation-> run()== true检查验证后将自定义消息添加到validation_errors()字符串

if(YOUR_CONDITION){
   $this->form_validation->run();
   $err = validation_errors();
   $err = $err. '<p>Custom validation error message</p>'. PHP_EOL;
   $data['err'] = $err;
   $this->load->view('viewname', $data)
}
else if ($this->form_validation->run() == true ) {
        #code...
}
else..

after setting your custom message to $err variable, print it on your view.

将自定义消息设置为$ err变量后,将其打印在视图上。

#5


-1  

You need to refer to the name of the field and not the rule.

您需要参考字段的名称而不是规则。

The docs are slightly confusing in that they also name it 'required'.

这些文档略显混乱,因为它们也将其命名为“必需”。

So where you have $this->form_validation->set_message('is_unique[users.username]', 'The username is already taken');

那么你有$ this-> form_validation-> set_message('is_unique [users.username]','用户名已被采用');

It should be

它应该是

$this->form_validation->set_message('username', 'The username is already taken');

$ this-> form_validation-> set_message('username','用户名已被占用');

Hope this helps!

希望这可以帮助!

#1


20  

Right way of doing this is by passing a string format

正确的方法是传递字符串格式

$this->form_validation->set_message('is_unique', 'The %s is already taken');

So, then only we can able to get message like "This Username is already taken" or "This Email is already taken".

那么,只有我们能够获得诸如“此用户名已被采用”或“此电子邮件已被采用”之类的消息。

#2


2  

This worked for me

这对我有用

$this->form_validation->set_message('is_unique', 'The username is already taken');

#3


2  

It's better you use like this:

你最好这样使用:

$this->form_validation->set_message('is_unique', 'The %s is already taken');

The form_validation changes %s with the label seted for the field.

form_validation使用为该字段设置的标签更改%s。

Hope this helps!

希望这可以帮助!

#4


0  

you can add your custom message to validation_errors() string after checking validation using $this->form_validation->run() == true

您可以在使用$ this-> form_validation-> run()== true检查验证后将自定义消息添加到validation_errors()字符串

if(YOUR_CONDITION){
   $this->form_validation->run();
   $err = validation_errors();
   $err = $err. '<p>Custom validation error message</p>'. PHP_EOL;
   $data['err'] = $err;
   $this->load->view('viewname', $data)
}
else if ($this->form_validation->run() == true ) {
        #code...
}
else..

after setting your custom message to $err variable, print it on your view.

将自定义消息设置为$ err变量后,将其打印在视图上。

#5


-1  

You need to refer to the name of the field and not the rule.

您需要参考字段的名称而不是规则。

The docs are slightly confusing in that they also name it 'required'.

这些文档略显混乱,因为它们也将其命名为“必需”。

So where you have $this->form_validation->set_message('is_unique[users.username]', 'The username is already taken');

那么你有$ this-> form_validation-> set_message('is_unique [users.username]','用户名已被采用');

It should be

它应该是

$this->form_validation->set_message('username', 'The username is already taken');

$ this-> form_validation-> set_message('username','用户名已被占用');

Hope this helps!

希望这可以帮助!