CodeIgniter配置文件中的自定义验证错误消息
我刚刚接触CodeIgniter(v 3.0.0)(来自CakePHP),我试图将自定义验证错误消息设置为我的一种形式。我使用配置文件存储所有我的验证规则,如此处。这是我的 application / config / form_validation.php
文件:
I'm new to CodeIgniter (v 3.0.0) (coming from CakePHP), and I'm trying to set custom validation error messages to one of my forms. I'm using a config file to store all my validation rules, as explained here. This is my application/config/form_validation.php
file:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
'appointments/signup' => array(
array(
'field' => 'admin[name]',
'label' => 'Name',
'rules' => 'required',
'errors' => array(
'required' => 'Please tell us your %s',
),
),
array(
'field' => 'admin[email]',
'label' => 'Email',
'rules' => 'required|valid_email|is_unique[users.email]',
'errors' => array(
'required' => 'Please enter your %s address',
'valid_email' => 'Please enter a valid email address',
'is_unique' => 'That email is already taken. Forgot your password?'
)
),
array(
'field' => 'admin[username]',
'label' => 'Username',
'rules' => 'required|min_length[4]|max_length[25]|is_unique[user_settings.username]',
'errors' => array(
'required' => 'Please choose a %s',
'min_length' => '%s must me at least 4 characters long',
'max_length' => '%s cannot exceen 25 characters',
'is_unique' => '%s is already taken :('
)
),
array(
'field' => 'admin[phone_number]',
'label' => 'Phone number',
'rules' => 'min_length[0]',
),
array(
'field' => 'admin[password]',
'label' => 'Password',
'rules' => 'required|min_length[8]',
'errors' => array(
'required' => 'Please choose a %s',
'min_length' => '%s must be at least 8 characters long'
)
),
array(
'field' => 'admin[passconf]',
'label' => 'Password',
'rules' => 'required|matches[admin[password]]',
'errors' => array(
'required' => 'Please re-type your %s',
'matches' => '%ss do not match'
)
),
array(
'field' => 'company[company_name]',
'label' => 'Organization\'s Name',
'rules' => 'required',
'errors' => array(
'required' => 'Please tell us your %s',
)
),
),
);
正如你所看到的,我试图使用 errors
数组,详细此处。但我仍然看到全局默认 The< field name>
As you can see, I'm trying to set custom validation feedback using the errors
array, as detailed here. But I still see the global default The <field name> field is required.
message.
有一种方法可以在配置文件中设置自定义验证消息,而无需编辑全局默认文件?
Is there a way to set custom validation messages in the config file, without having to edit the global default file?
尝试更改数组中键的顺序,如下所示:
Try to change the order of the keys in your array, something like this:
'appointments/signup' => array(
array(
'field' => 'admin[name]',
'label' => 'Name',
'errors' => array(
'required' => 'Please tell us your %s',
),
'rules' => 'required',
)
完全相同的问题发生在我身上,在核心类上进行了一些调试之后,我感觉很蠢, 。
The exact same problem was happening to me, and after some debugging on the core classes, I was feeling stupid enough to try this.
看起来像一个错误,但我没有进一步。
Looks like a bug, but I didn't go any further.
3.0.1。
UPDATE
我错了,如果这是发生在v 3.0.0,不是发生在3.0.1。我上面描述的是我在数组中的括号错误。
I was wrong, if this was happening on v 3.0.0, is not happening on 3.0.1. What I described above was me making a mistake with parentheses in my array.
一切都正常工作。