简体   繁体   English

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

[英]Custom error message using CodeIgniter Form Validation

I want to make some custom error messages in my CodeIgniter forms. I've tried using我想在我的 CodeIgniter forms 中制作一些自定义错误消息。我试过使用

$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.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_uniqueThe username is already taken for usernames, and The e-mail is already registered for mails.

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]');
}

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""This Email is already taken"

这对我有用

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

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! 希望这可以帮助!

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

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

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变量后,将其打印在视图上。

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

You can pass the third parameter to set messages for the validation您可以传递第三个参数来设置验证消息

$this->form_validation->set_rules(
        'username', 'Username',
        'required|min_length[5]|max_length[12]|is_unique[users.username]',
        array(
                'required'      => 'You have not provided %s.',
                'is_unique'     => 'This %s already exists.'
        )
);

Check here 在这里查看

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');

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM