PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 8, 2022

[FIXED] Codeigniter - form validation

 February 08, 2022     codeigniter, codeigniter-form-validation, forms, validation     No comments   

Issue

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

Solution

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


Answered By - Micky
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing