Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating CodeIgniter's form validation error messages

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

like image 318
John Avatar asked Jan 30 '12 20:01

John


People also ask

What is validation error message?

Validation errors typically occur when a request is malformed -- usually because a field has not been given the correct value type, or the JSON is misformatted.

How do you validate CI?

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();

Which rules returns false if the form element is empty?

Validation Rule Reference. Given below are the most commonly used list of native rules available to use. Returns FALSE if the form element is empty. Returns FALSE if the form element does not match the one in the parameter.


1 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

like image 96
Wes Crow Avatar answered Oct 05 '22 23:10

Wes Crow