Welcome Guest, Not a member yet? Register   Sign In
Custom validation error messages.
#1

[eluser]loathsome[/eluser]
Hi there.

How can I have totally custom error messages when a form does not validate? E.g. I want to have one for nick and one for comment: You have to type a nick! -- I can not accept this without a comment.

I've tried something like this:

Code:
$rules['nick'] = 'required';
$rules['comment'] = 'required|min_length[10]';

$field['nick'] = 'You have to type a nick';
$field['comment'] = 'I can not accept this without a comment';

$this->validation->set_rules($rules);
$this->validation->set_message('required', '%s');

Which actually works - but what do I do when it comes to validation of the length? As it is now, it will display "The I can not accept this without a comment field needs to contain at least blabla characters" which is obviously not what I want.

Sorry if I explained this a bit messy, but hopefully you'll get the idea.
#2

[eluser]Colin Williams[/eluser]
I would extend the Validation class, and create a set_errors() method that lets you do this like:

Code:
$error['nick'] = 'You have to type a nick';
$error['comment'] = 'I can not accept this without a comment';

$this->validation->set_errors($error);

I almost think the Validation library should become a Form library, where validation is just a feature. It's such a good system but it really only does a few things. It could do a lot more.
#3

[eluser]loathsome[/eluser]
That's a great idea! I will have a go at it later.

Quote:I almost think the Validation library should become a Form library, where validation is just a feature. It’s such a good system but it really only does a few things. It could do a lot more.

You're absolutely right.
#4

[eluser]Colin Williams[/eluser]
If you haven't done it yet, I got this working.

Code:
<?php

class MY_Validation extends CI_Validation {

   function MY_Validation()
   {
      parent::CI_Validation();
   }
  
   function set_errors($fields)
   {
      if (is_array($fields) and count($fields))
      {
         foreach($fields as $key => $val)
         {
            $error = $key.'_error';
            if (isset($this->$error) and isset($this->$key) and $this->$error != '')
            {
               $old_error = $this->$error;
               $new_error = $this->_error_prefix.sprintf($val, $this->$key).$this->_error_suffix;
               $this->error_string = str_replace($old_error, $new_error, $this->error_string);
               $this->$error = $new_error;
            }
         }
      }    
   }
  
}

This creates $this->validation->set_errors(). Because the run() method generates the error string based on your rules, this method MUST be called AFTER $this->validation->run() and BEFORE your store or print $this->validation->error_string:

Code:
[...]
      // Run Validation
      if (!$this->validation->run())
      {
         // Set custom errors
         $this->validation->set_errors(array('name' => 'Common now!'));
        
         if (!empty($this->validation->error_string))
         // Validation ran and there was an error
         {
            $form['error'] = $this->validation->error_string;
[...]

I admit, it's a bit awkward using str_replace(), but I'd never want to override the run() method, which generates errors based on rules and not fields.

Also note that your supplied messages get sprintf'd, so you can include the submitted value in your error!

Code:
$this->validation->set_errors(array('email' => "'%s' is not a valid email address."));

// "'colin@website' is not a valid email address."

Remember to use %1$s if the field value appears more than once in your message.
#5

[eluser]loathsome[/eluser]
Totally awesome! Thanks a lot for your very helpful reply.

Cheers!
#6

[eluser]Colin Williams[/eluser]
Sure thing, loathsome. I actually needed to code this for a current project I'm doing for my mom ("Those error messages it gives are boring!"). I hope it serves you well! Let us know if you improve it.
#7

[eluser]loathsome[/eluser]
Your mom actually? Smile Mind sharing what you're creating?
#8

[eluser]Colin Williams[/eluser]
Yep, me mums. She recently decided to follow a life-long dream of publishing a women's fitness-type magazine aimed at 40+ year-old women. I'm helping her out by designing and building the Web site. Mainly just static pages which I'm serving up with a REALLY rudimentary CMS, and then it's got a robust contact form and a subscription/subscriber management system. I'll PM you the URL where you can peek it. It launches Thurs (well, that's the plan..).
#9

[eluser]jt`[/eluser]
Mad props for the custom error library. Works like a charm!
#10

[eluser]Colin Williams[/eluser]
[quote author="jt`" date="1216205096"]Mad props for the custom error library. Works like a charm![/quote]

Thanks JT! I hope it serves you and others well. I wonder if this should go into the Wiki?




Theme © iAndrew 2016 - Forum software by © MyBB