简体   繁体   English

CodeIgniter配置文件中的自定义验证错误消息

[英]Custom validation error messages in CodeIgniter config file

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. 我是CodeIgniter(v 3.0.0)的新手(来自CakePHP),我正在尝试将自定义验证错误消息设置为我的一个表单。 I'm using a config file to store all my validation rules, as explained here . 我使用一个配置文件来存储我所有的验证规则,如解释在这里 This is my application/config/form_validation.php file: 这是我的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',
            )
        ),
    ),
);

As you can see, I'm trying to set custom validation feedback using the errors array, as detailed here . 如您所见,我正在尝试使用errors数组设置自定义验证反馈,如此处所述 But I still see the global default The <field name> field is required. 但我仍然看到全局默认值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. 看起来像一个bug,但我没有再进一步了。

I'm using version 3.0.1. 我使用的是3.0.1版本。



UPDATE UPDATE

I was wrong, if this was happening on v 3.0.0, is not happening on 3.0.1. 我错了,如果这是在3.0.0上发生的,则不会发生在3.0.1上。 What I described above was me making a mistake with parentheses in my array. 我上面描述的是我在数组中用括号做错了。

Everything is working as it should. 一切都在发挥作用。

validation error messages comes from language files because each language has own error messages 验证错误消息来自语言文件,因为每种语言都有自己的错误消息

I think you can change validation error messages in language files. 我认为您可以更改语言文件中的验证错误消息。

Please try use helpers under application/helpers and define your validation errors in function. 请尝试在application / helpers下使用帮助程序,并在函数中定义验证错误。 Then try accessing the validation rules or use errors under application/errors. 然后尝试访问验证规则或使用应用程序/错误下的错误。 please refer https://ellislab.com/codeigniter/user-guide/general/helpers.html 请参阅https://ellislab.com/codeigniter/user-guide/general/helpers.html

也许你应该将你的关键字段放在引号中,如:

'field' => "admin['name']"

Firstly, make sure you are using Codeigniter 3 not any version of Codeigniter 2.xx . 首先,确保使用Codeigniter 3而不是任何版本的Codeigniter 2.xx

I was in trouble with the same issue and found that the errors array is available in Codeigniter 3 version and the config rules are set in form_validation's run() method, so if you see the set_rules function in Form_validation.php file you will see the 4 th parameter which is errors 我遇到了同样的问题,发现errors数组在Codeigniter 3版本中可用,配置规则在form_validation的run()方法中设置,所以如果你在Form_validation.php文件中看到set_rules函数,你会看到4 这个参数是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())
{
   .....

And which is not available in 2.2-stable version , see Form_validation.php , and see the piece of code which shows the difference 哪个在2.2稳定版本中不可用,请参阅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 = '')
{
   ....

Do not try use direct call to signup(your_func_name). 不要尝试使用直接调用注册(your_func_name)。 $this->form_validation->run('signup') $这个 - > form_validation->运行( '注册')。

Use alternate method - (controller_name/function_name) 使用备用方法 - (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'
    )

)); ));

then call like it, 然后像这样打电话,

if ($this->form_validation->run() == FALSE) {... } if($ this-> form_validation-> run()== FALSE){...}

cheers!!! 干杯!!!

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

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