我是CodeIgniter(v 3.0.0)的新手(来自CakePHP),我正在尝试将自定义验证错误消息设置为我的一个表单。我正在使用配置文件来存储我的所有验证规则,如here所述。这是我的application/config/form_validation.php
文件:
<?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
数组设置自定义验证反馈,详细信息为here。但我仍然看到全局默认的The <field name> field is required.
消息。
有没有办法在配置文件中设置自定义验证消息,而无需编辑全局默认文件?
答案 0 :(得分:4)
尝试更改数组中键的顺序,如下所示:
'appointments/signup' => array(
array(
'field' => 'admin[name]',
'label' => 'Name',
'errors' => array(
'required' => 'Please tell us your %s',
),
'rules' => 'required',
)
我遇到了完全相同的问题,经过核心课程的一些调试后,我感觉很愚蠢,试试这个。
看起来像个错误,但我没有再深入了。
我使用的是3.0.1版。
的更新强>
我错了,如果在3.0.0上发生这种情况,则不会在3.0.1上发生。我上面描述的是我在数组中用括号做错了。
一切正常。
答案 1 :(得分:2)
验证错误消息来自语言文件,因为每种语言都有自己的错误消息
我认为您可以更改语言文件中的验证错误消息。
答案 2 :(得分:0)
请尝试在应用程序/帮助程序下使用帮助程序,并在功能中定义验证错误。然后尝试访问验证规则或使用应用程序/错误下的错误。请参阅https://ellislab.com/codeigniter/user-guide/general/helpers.html
答案 3 :(得分:0)
可能你应该将你的关键字段放在引号中,如:
'field' => "admin['name']"
答案 4 :(得分:0)
首先,请确保您使用Codeigniter 3
而不是Codeigniter 2.x.x
的任何版本。
我遇到了同样的问题,发现errors
数组在Codeigniter 3 version中可用,并且配置规则是在form_validation的run()方法中设置的,所以如果你看到了Form_validation.php文件中的set_rules函数,您将看到4 {sup> th 参数errors
/**
* Set Rules
*
* This function takes an array of field names and validation
* rules as input, any custom error messages, validates the info,
* and stores it
*
* @param mixed $field
* @param string $label
* @param mixed $rules
* @param array $errors
* @return CI_Form_validation
*/
public function set_rules($field, $label = '', $rules = array(), $errors = array())
{
.....
2.2-stable version 中没有,请参阅Form_validation.php,并查看显示差异的代码
/**
* Set Rules
*
* This function takes an array of field names and validation
* rules as input, validates the info, and stores it
*
* @access public
* @param mixed
* @param string
* @return void
*/
public function set_rules($field, $label = '', $rules = '')
{
....
答案 5 :(得分:0)
不要尝试使用直接调用注册(your_func_name)。 $这 - &GT; form_validation-&gt;运行(&#39;注册&#39)
使用备用方法 - (controller_name / function_name)
$config = array(
'Authenticate_user/signup' => array(
array(
'field' => 'firstname',
'label' => 'Name',
'rules' => 'trim|required'
),
array(
'field' => 'useremail',
'label' => 'Email ID',
'rules' => 'trim|required|callback_check_unique_emailid'
),
array(
'field' => 'gender',
'label' => 'Gender',
'rules' => 'trim|required'
),
array(
'field' => 'age',
'label' => 'Age',
'rules' => 'trim|required'
),
array(
'field' => 'passcode',
'label' => 'Password',
'rules' => 'trim|required'
),
array(
'field' => 'confirmpasscode',
'label' => 'Confirm Password',
'rules' => 'required|matches[passcode]',
'errors' => array(
'matches[passcode]' => 'Only number is allowed'
)
),
array(
'field' => 'usertype',
'label' => 'User Type',
'rules' => 'trim|required'
),
array(
'field' => 'country',
'label' => 'Country',
'rules' => 'trim|required'
),
array(
'field' => 'state',
'label' => 'State',
'rules' => 'trim|required'
),
array(
'field' => 'category',
'label' => 'Category',
'rules' => 'required'
)
));
然后像这样打电话,
if($ this-&gt; form_validation-&gt; run()== FALSE){...}
欢呼声!!!