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

8 Answers 8

35

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

Sign up to request clarification or add additional context in comments.

1 Comment

What if i want to replace the '%s' with the actual input value? Ex. 'Foo' is already taken...
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'));

Comments

3

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!

Comments

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

Comments

2

This worked for me

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

Comments

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.

Comments

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

Comments

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?',
        ],
    ],
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.