Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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',
            )
        ),
    ),
);

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?

like image 411
Agent.Logic_ Avatar asked Apr 16 '15 11:04

Agent.Logic_


People also ask

How to set error message in CodeIgniter?

PHP_EOL; $data['err'] = $err; $this->load->view('viewname', $data); } else if ($this->form_validation->run() == true ) { #code... } else.. after setting your custom message to $err variable, print it on your view. Save this answer.

How to set validation in CodeIgniter?

Setting Validation Rules CodeIgniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data at the same time. To set validation rules you will use the set_rules() method: $this->form_validation->set_rules();


2 Answers

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.

I'm using version 3.0.1.



UPDATE

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.

Everything is working as it should.

like image 128
Catapimba Avatar answered Nov 03 '22 00:11

Catapimba


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.

like image 22
Mahdi Majidzadeh Avatar answered Nov 03 '22 00:11

Mahdi Majidzadeh