[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!