Welcome Guest, Not a member yet? Register   Sign In
Form validation error message for length validations
#1

[eluser]helmutbjorg[/eluser]
I'm using the form_validation library to test for min_length, max_length, and exact_length and the following error messages are being returned...

Code:
The title field must be at least 200 characters in length.
The title field can not exceed 200 characters in length.
The title field must be exactly 200 characters in length.

This is good. But not great. It is better in these sort of error messages to give the user some ideas of how they could fix the problem. Like so...

Code:
The title field must be at least 200 characters in length. You have 140 characters.
The title field can not exceed 200 characters in length. You have 205 characters.
The title field must be exactly 200 characters in length. You have 205 characters.

Now what would be the best way of getting that result? The way error messages are constructed in the form_validation library only allows for max of two '%s' sprintf params.
#2

[eluser]helmutbjorg[/eluser]
Okay... i figured out a solution to the problem. Thought i'd share it with you guys.

Extend the form_validation library by creating the following file:

system/application/libraries/MY_Form_validation.php
Code:
class MY_Form_validation extends CI_Form_validation {

    function MY_Form_validation() {
        parent::CI_Form_validation();
    }

    // Custom max length
    function my_max_length($str, $val) {
        if(!$this->max_length($str, $val)) {
            $this->set_message('my_max_length', str_relace('[count]', strlen($str), $this->CI->lang->line('my_max_length')));
            return false;
        }
        return true;
    }
    
    // Custom min length
    function my_min_length($str, $val) {
        if(!$this->min_length($str, $val)) {
            $this->set_message('my_min_length', str_replace('[count]', strlen($str), $this->CI->lang->line('my_min_length')));
            return false;
        }
        return true;
    }    

    // Exact Length
    function my_exact_length($str, $val) {
        if(!$this->exact_length($str, $val)) {
            $this->set_message('my_exact_length', str_replace('[count]', strlen($str), $this->CI->lang->line('my_exact_length')));
            return false;
        }
        return true;
    }    


}

And then you create a custom language file:

system/application/language/english/MY_form_validation_lang.php
Code:
$lang['my_min_length']        = "The %s field must be at least %s characters in length. You have [count] characters.";
$lang['my_max_length']         = "The %s field can not exceed %s characters in length. You have [count] characters.";
$lang['my_exact_length']    = "The %s field must be exactly %s characters in length. You have [count] characters.";

Then in your code you can use the following...
Code:
$this->form_validation->set_rules('title', 'title', 'trim|required|my_max_length[20]|xss_clean');

Cheers!
#3

[eluser]helmutbjorg[/eluser]
On a follow up note, you can do it a better way like so...

Code:
// Custom max length
function max_length($str, $val) {
    if(!parent::max_length($str, $val)) {
        $this->set_message('max_length', str_relace('[count]', strlen($str), $this->CI->lang->line('my_max_length')));
        return false;
    }
    return true;
}

This way all of your existing code will still work! You can still use max_length (instead of my_max_length) in your validation lists. But the newer (more helpful) error message will show too!!
#4

[eluser]aprium[/eluser]
Thanks, found this really handy.
#5

[eluser]jonnny[/eluser]
A slight tweak makes it work with the set_message() function if you cant use the lang file.

I wanted generic messages, and %s would put the field name in, not the max length that I wanted.

Code:
function max_length($str, $val)
{
if(!parent::max_length($str, $val))
{
  $this->set_message('max_length', str_replace('[max]', $val, $this->_error_messages['max_length']));
  return false;
}
return true;
}

$this->form_validation->set_message('max_length', 'This field can not exceed [max] characters');

#6

[eluser]jonnny[/eluser]
Even better:

Code:
function max_length($str, $val)
{
  if(!parent::max_length($str, $val))
  {
   $err_msg = (isset($this->_error_messages['max_length'])) ? $this->_error_messages['max_length']:$this->CI->lang->line('max_length');
   $this->set_message('max_length', str_replace('[max]', $val, $err_msg));
   return false;
  }
  return true;
}

I was occasionally getting errors because $this->_error_messages['max_length'] did not exist (the times when I had not manually changed the error message in advance). This fixes that.




Theme © iAndrew 2016 - Forum software by © MyBB