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 %?'));
2
  • 1
    Multiple rules should be separated by pipes, right? So ..., 'required|max_length[255]', array(...) ? Commented Aug 4, 2021 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 ? Commented Aug 5, 2021 at 5:28

2 Answers 2

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"
        )
);
Sign up to request clarification or add additional context in comments.

Comments

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'
        )
);

1 Comment

what if i change max_length[255] to max_length[100] then i have to change array rules manually. This answer is not helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.