Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:29:45+00:00 2026-05-29T05:29:45+00:00

i’m trying to setup a codeigniter form with different error messages. set_message(rule, msg) is

  • 0

i’m trying to setup a codeigniter form with different error messages.

set_message(rule, msg) is setting up a message for the whole form.

I need:

$this->form_validation->set_rules('name', 'First Name', 'required');
$this->form_validation->set_message('name', 'required', 'Enter your Name');    
$this->form_validation->set_rules('second', 'Variables', 'required');
$this->form_validation->set_message('second', 'required', 
                                    'The Variables are required');

Adding the %s into the message string is no help in this case, since the messages have to be completely different.

Possibly I could do something like:

controller

$this->form_validation->set_rules('name', 'Name',
                                  'required|min_length[6]|max_length[12]');
$this->form_validation->set_rules('second', 'Variables',
                                  'required|min_length[3]|max_length[5]');
$this->form_validation->set_message('required', 'required');
$this->form_validation->set_message('min_length', 'short');
$this->form_validation->set_message('max_length', 'long');

view

switch(form_error('name')) {
    case '<p>required</p>':
        echo 'Enter your Name';
        break;
    case '<p>short</p>':
        echo 'min length 6';
        break;
    case '<p>long</p>':
        echo 'min length 12';
        break;

}

switch(form_error('second')) {
    case '<p>required</p>':
        echo 'The Variables are required';
        break;
    case '<p>short</p>':
        echo 'min length 3';
        break;
    case '<p>long</p>':
        echo 'min length 5';
        break;

}

But isn’t there a smarter way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:29:45+00:00Added an answer on May 29, 2026 at 5:29 am

    I think a smarter way would be to use Codeigniter’s callback feature (something similar to below). The following works but it may be possible to streamline it even more. If nothing else, it’s a starting point.

    Create two callback functions (I’ve named these custom_required and custom_check_length) and place them at the bottom of your controller (or wherever you feel necessary).

    private function _custom_required($str, $func) {
            switch($func) {
                case 'name':
                    $this->form_validation->set_message('custom_required', 'Enter your name');
                    return (trim($str) == '') ? FALSE : TRUE;
                    break;
                case 'second':
                    $this->form_validation->set_message('custom_required', 'The variables are required');
                    return (trim($str) == '') ? FALSE : TRUE;
                    break;
            }
        }
    

    and…

    private function _custom_check_length($str, $params) {
            $val = explode(',', $params);
    
            $min = $val[0];
            $max = $val[1];
    
            if(strlen($str) <= $max && strlen($str) >= $min) {
                return TRUE;
            } elseif(strlen($str) < $min) {
                $this->form_validation->set_message('custom_check_length', 'Min length ' . $min);
                return FALSE;
            } elseif(strlen($str) > $max) {
                $this->form_validation->set_message('custom_check_length', 'Max length ' . $max);
                return FALSE;
            }
        }
    

    These two functions take care of the set_message aspect of your form validation. To set the rules, you simply need to call these two functions by prefixing the function name with callback_.

    So…

    $this->form_validation->set_rules('name', 'Name', 'callback__custom_required[name]|callback__custom_check_length[6,12]');
    $this->form_validation->set_rules('second', 'Second', 'callback__custom_required[second]|callback__custom_check_length[3,5]');
    

    I hope the above helps in some way!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.