Custom Validation Error Messages in CodeIgniter Configuration File

I am new to CodeIgniter (v 3.0.0) (comes from CakePHP) and I am trying to set authentication error messages in one of my forms. I use the configuration file to store all my validation rules, as described here . This is my 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\ Name', 'rules' => 'required', 'errors' => array( 'required' => 'Please tell us your %s', ) ), ), ); 

As you can see, I am trying to set a custom reverse validation using the errors array, as described below here . But I still see the default global message The <field name> field is required. .

Is there a way to set custom validation messages in a configuration file without having to edit the default global file?

+7
php codeigniter
source share
6 answers

Try changing 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 same thing happened to me, and after some debugging on the main classes, I felt stupid enough to try this.

Looks like a mistake, but I no longer went.

I am using version 3.0.1.



UPDATE

I was mistaken if this happened in version 3.0.0, this does not happen in 3.0.1. What I described above was a bug with brackets in my array.

Everything works as it should.

+4
source share

validation error messages come from language files, since each language has its own error messages

I think you can change the validation error messages in the language files.

+2
source share

Try using helpers under the application / helpers and identify your validation errors in the function. Then try to look at the validation rules or use the errors in the application / errors. please refer https://ellislab.com/codeigniter/user-guide/general/helpers.html

0
source share

Maybe you should put your key field in quotation marks, for example:

 'field' => "admin['name']" 
0
source share

First, make sure that you are using Codeigniter 3 not any version of Codeigniter 2.xx

I had a problem with the same problem and it turned out that the errors array is available in Codeigniter 3 and the configuration rules are set in the form_validation run () method, so if you see the set_rules function in the Form_validation.php file, you will see 4 th which is equal to 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 the 2.2 stable version , see Form_validation.php and see the code snippet that shows the difference

 /** * 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 = '') { .... 
0
source share

Do not try to use a direct call to register (your_func_name). $ This-> form_validation-> Run ('Registration')

Use an alternative method - (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 him

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

Hooray!!!

0
source share

All Articles