I want to make some custom error messages in my CodeIgniter forms. I've tried using
$this->form_validation->set_message('is_unique[users.username]', 'The username is already taken');
However I can't get it working.
Editing the form_validation_lang.php
file is not good enough, as is_unique
will be The username is already taken for usernames, and The e-mail is already registered for mails.
How can I make this custom error message?
Here's a snippet from my code:
$this->form_validation->set_message('is_unique[users.username]', 'The username is already taken');
// Check if username has changed
if ($this->input->post('username') !== $user->username) {
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[20]|is_unique[users.username]');
}
Show activity on this post. if(YOUR_CONDITION){ $this->form_validation->run(); $err = validation_errors(); $err = $err. '<p>Custom validation error message</p>'. PHP_EOL; $data['err'] = $err; $this->load->view('viewname', $data); } else if ($this->form_validation->run() == true ) { #code... } else..
Show Errors in Codeigniter To easily debug the application, we will enable the error reporting in our Codeigniter app. Open app/Config/Boot/development. php file and set the display_errors to 1 instead of 0.
Create a view file myform. php and save the below code it in application/views/myform. php. This page will display form where user can submit his name and we will validate this page to ensure that it should not be empty while submitting.
Right way of doing this is by passing a string format
$this->form_validation->set_message('is_unique', 'The %s is already taken');
So, then only we can able to get message like "This Username is already taken"
or "This Email is already taken"
.
This is how you set a message error ONLY for username:
$this->form_validation->set_rules('username','Username','is_unique',array('is_unique' => 'The %s is already taken'));
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