Translation of code form validation error messages

Is it necessary in any case to translate CodeIgniter form validation error messages without touching the system files?

+7
source share
2 answers

If you are talking about translating into another language, this can be done by setting the configuration value $config['language'] to the desired language. If you do not want to modify the actual config.php file, you can do this using the Config object set_item() function as follows:

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

See: CodeIgniter Doc for configuration class

It is assumed that you have a Spanish directory in the language directory, at least with the form_validation_lang.php file.

However, if you just want to create your own messages for the Form_validation object, you can copy the form_validation_lang.php file from the system\language directory and transfer it to the application\language directory. Now you can edit the new language file to reflect any other messages that you want. You can also easily revert to default messages by deleting the file from the application/language directory.

Another way to do this if you do not want to touch even language files is to manually redefine the messages. You can do this through the library object Form_validation as follows:

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

See: CodeIgniter Doc for form validation class

+26
source

If you need to set your own error message for a specific field in a specific 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 problem with any fields regardless. :)

0
source

All Articles