Is there anyway to translate CodeIgniter's form validation error messages without touching to system files?
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.
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();
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.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With