Codeigniter - form validation

85 views Asked by At

I am validating form in Codeigniter using form_validation library and setting rules using an array.

I want validation message on max_length like this:

Username may not be greater than 255 character.

%s may not be greater than %? character.

The problem is I am getting Username using %s but I want 255 dynamically in validation message.

$this->form_validation->set_rules('username', 'Username', 'required|max_length[255]',
    array('required' => 'You must provide a %s.','max_length' => '%s may not be greater than %?'));
2

There are 2 answers

0
Micky On BEST ANSWER

I don't have much reputation points so posting this as an answer.
This can be achieved by using variable

$max_len = 100; // Set max length here 
$this->form_validation->set_rules(
        'username', 'Username', "required|max_length[$max_len]",
        array(
                'required'      => 'You have not provided %s.',
                'max_length'     => "%s may not be greater than $max_len"
        )
);
1
Ganpat Thakor On
$this->form_validation->set_rules(
        'username', 'Username',
        'required|max_length[255]',
        array(
                'required'      => 'You have not provided %s.',
                'max_length'     => '%s may not be greater than 255'
        )
);