Home » Php » php – Translating CodeIgniter's form validation error messages

php – Translating CodeIgniter's form validation error messages

Posted by: admin July 12, 2020 Leave a comment


Is there anyway to translate CodeIgniter’s form validation error messages without touching to system files?

How to&Answers:

If you are talking about actually translating to another language, this can be done by setting the config value $config['language'] to the language you want. If you don’t want to change the actual config.php file you can do it through the use of the Config object’s set_item() function like this:

$this->config->set_item('language', 'spanish');

See: CodeIgniter Doc for the Config Class

This assumes that you have a spanish directory in your language directory with at least the form_validation_lang.php file.

However, if you are just wanting to create custom messages for the Form_validation object, you can copy the form_validation_lang.php file from the system\language directory and move it to the application\language directory. You can now edit the new language file to make it reflect any different messages you want. You can also revert back to the default messages easily by removing the file from the application/language directory.

Another way to do it, if you don’t want to touch even the language files is to manually override the messages. You can do that through the Form_validation library object like so:

$this->form_validation->set_message('required', 'This is a required item!');`

See: CodeIgniter Doc for the Form Validation Class


If you need to set a custom error message for a particular field on some particular rule, use the set_rules() method:

    $this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3',
        array('rule2' => 'Error Message on rule2 for this field_name')

This will solve your all fields problem independently. 🙂