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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14460620/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:19:07  来源:igfitidea点击:

Custom error message using CodeIgniter Form Validation

phpformscodeignitervalidation

提问by Patrick Reck

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]', 'The username is already taken');

However I can't get it working.

但是我无法让它工作。

Editing the form_validation_lang.phpfile is not good enough, as is_uniquewill be The username is already takenfor usernames, and The e-mail is already registeredfor 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]');
}

回答by bkmagnetron

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

所以,只有我们才能得到像"This Username is already taken"or 之类的消息"This Email is already taken"

回答by Carlos Nogueira

It's better you use like this:

最好这样使用:

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

The form_validation changes %swith the label seted for the field.

form_validation 随%s为该字段设置的标签而变化。

Hope this helps!

希望这可以帮助!

回答by Andreea Onica

This is how you set a message error ONLY for username:

这是您仅为用户名设置消息错误的方式:

$this->form_validation->set_rules('username','Username','is_unique',array('is_unique' => 'The %s is already taken'));

回答by Devendra Verma

This worked for me

这对我有用

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

回答by Narendra Gupta

create a **form_validation.php** file and use this method:

"add_user_rule"   => [
    [
        "field" => "username",
        "label" => "Username",
        "rules" => "required|trim|is_unique[users.username]",
        "errors" => [
            'is_unique' => 'The %s is already taken.',
        ],
    ],
],

Thank You

回答by Badiparmagi

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

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

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 $errvariable, print it on your view.

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

回答by Chris Till

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]', 'The username is already taken');

It should be

它应该是

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

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

Hope this helps!

希望这可以帮助!