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

Pat*_*eck 8 php forms validation codeigniter

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

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

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

编辑form_validation_lang.php文件不够好,is_unique用户名已被占用的用户名和电子邮件已被注册的邮件.

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

这是我代码中的代码段:

$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]');
}
Run Code Online (Sandbox Code Playgroud)

bkm*_*ron 23

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

$this->form_validation->set_message('is_unique', 'The %s is already taken');
Run Code Online (Sandbox Code Playgroud)

那么,只有我们能够得到像"This Username is already taken"或的消息"This Email is already taken".


小智 5

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

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