Custom error message using CodeIgniter Form Validation
Asked Answered
M

8

16

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]');
}
Metchnikoff answered 22/1, 2013 at 14:15 Comment(2)
check out the form validation example on the codeigniter documentation site.. ellislab.com/codeigniter/user-guide/libraries/…Deeannadeeanne
The code example I posted is identical to the docs: $this->form_validation->set_message('required', 'Your custom message here'); Metchnikoff
G
34

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

Gurnard answered 17/3, 2014 at 17:45 Comment(1)
What if i want to replace the '%s' with the actual input value? Ex. 'Foo' is already taken...Hereditament
D
6

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'));
Doubletree answered 5/2, 2019 at 10:54 Comment(0)
P
3
create a **form_validation.php** file and use this method:

"add_user_rule"   => [
    [
        "field" => "username",
        "label" => "Username",
        "rules" => "required|trim|is_unique[users.username]",
        "errors" => [
            'is_unique' => 'The %s is already taken.',
        ],
    ],
],

Thank You
Porterporterage answered 10/4, 2019 at 6:3 Comment(0)
H
2

This worked for me

$this->form_validation->set_message('is_unique', 'The username is already taken');
Homerus answered 19/4, 2013 at 7:57 Comment(0)
J
2

It's better you use like this:

$this->form_validation->set_message('is_unique', 'The %s is already taken');

The form_validation changes %s with the label seted for the field.

Hope this helps!

Jacquettajacquette answered 30/4, 2014 at 16:52 Comment(0)
C
0

you can add your custom message to validation_errors() string after checking validation using $this->form_validation->run() == true

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

after setting your custom message to $err variable, print it on your view.

Candycandyce answered 26/12, 2017 at 10:21 Comment(0)
P
0

You can pass the third parameter to set messages for the validation

$this->form_validation->set_rules(
        'username', 'Username',
        'required|min_length[5]|max_length[12]|is_unique[users.username]',
        array(
                'required'      => 'You have not provided %s.',
                'is_unique'     => 'This %s already exists.'
        )
);

Check here

Propound answered 3/12, 2021 at 4:52 Comment(0)
D
0

This is example of CI4 of custom error and replace value and field according input.

$validation->setRules([
    'username' => [
        'label'  => 'Username',
        'rules'  => 'required|max_length[30]|is_unique[users.username]',
        'errors' => [
            'required' => 'All accounts must have {field} provided',
        ],
    ],
    'password' => [
        'label'  => 'Password',
        'rules'  => 'required|max_length[255]|min_length[10]',
        'errors' => [
            'min_length' => 'Your {field} is too short. You want to get hacked?',
        ],
    ],
]);
Dialectical answered 10/10, 2023 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.