When using CodeIgniter I like to set my error messages in application/language/english/form_validation_lang.php
which works fine for every error message but does not seem to work for the is_unique
message as it gives me the standard message of "The email field must contain a unique value."
My code:
$lang['is_unique'] = "The %s entered is already in use.";
In your CodeIgniter system folder you'll find one called language containing sets of language files. You can create your own language files as needed in order to display error and other messages in other languages. Language files are typically stored in your system/language directory.
CodeIgniter provides a comprehensive form validation and data prepping class that helps minimize the amount of code you’ll write. Try it! Before explaining CodeIgniter’s approach to data validation, let’s describe the ideal scenario: A form is displayed. You fill it in and submit it.
Inside this article we will see the concept of CodeIgniter 4 Language Localization. Simply means creating a site in CodeIgniter 4 but in different different language. This article will be very interesting to learn and easy to implement in your code. Let’s get started – CodeIgniter 4 Website in Multi Language.
When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env Either we can do via renaming file as simple as that. Also we can do by terminal command. Above command will create a copy of env file to .env file.
Create a file called form_validation_lang.php
in so like below
Go to system/language/english/form_validation_lang.php find.
$lang['form_validation_is_unique'] = 'The {field} field must contain a unique value.';
Copy Key Above Then Add Into application/language/english/form_validation_lang.php
$lang['form_validation_is_unique'] = 'The {field} entered is already in use.';
Next
On Controller Form Validation Add
$this->lang->load('form_validation', 'english');
Like
$this->lang->load('form_validation', 'english');
$this->form_validation->set_rules('username', 'Username', 'trim|required|is_unique[users.username]');
Note: If you use the form_validation is_unique in other controllers and want to use that message you will need to load this $this->lang->load('form_validation', 'english');
On to that controller as well unless you choose to autoload it.
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