Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom error message using CodeIgniter Form Validation

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]');
}
like image 850
Patrick Reck Avatar asked Jan 22 '13 14:01

Patrick Reck


People also ask

How to set custom error message in CodeIgniter form validation?

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..

How to show validation error in CodeIgniter 4?

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.

How to give form validation in CodeIgniter?

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.


2 Answers

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".

like image 179
Balakrishnan Avatar answered Sep 21 '22 07:09

Balakrishnan


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'));
like image 31
Andreea Onica Avatar answered Sep 22 '22 07:09

Andreea Onica