Welcome Guest, Not a member yet? Register   Sign In
Form validation error message for length validations
#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!


Messages In This Thread
Form validation error message for length validations - by El Forum - 01-11-2009, 06:48 PM
Form validation error message for length validations - by El Forum - 01-12-2009, 03:19 AM
Form validation error message for length validations - by El Forum - 01-15-2009, 08:44 AM
Form validation error message for length validations - by El Forum - 12-03-2010, 07:41 AM
Form validation error message for length validations - by El Forum - 04-23-2013, 06:27 PM
Form validation error message for length validations - by El Forum - 05-02-2013, 10:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB