0

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 %?'));
omkar
  • 47
  • 11
  • 1
    [Multiple rules should be separated by pipes, right](https://codeigniter.com/userguide3/libraries/form_validation.html#cascading-rules)? So `..., 'required|max_length[255]', array(...)` ? – Don't Panic Aug 04 '21 at 16:16
  • @Don't Panic yes, you are right multiple rules separated by pipes, by mistakenly typed comma but what is the solution on this question ? – omkar Aug 05 '21 at 05:28

2 Answers2

1

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"
        )
);
Micky
  • 107
  • 3
0
$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'
        )
);
  • what if i change ``max_length[255]`` to ``max_length[100]`` then i have to change array rules manually. This answer is not helping. – omkar Aug 05 '21 at 10:04