Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I setup custom error messages for each form field in Codeigniter?

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?

like image 553
s3mre8 Avatar asked Feb 02 '12 07:02

s3mre8


People also ask

How to set custom Error message in CodeIgniter form validation?

PHP_EOL; $data['err'] = $err; $this->load->view('viewname', $data); } else if ($this->form_validation->run() == true ) { #code... } else.. after setting your custom message to $err variable, print it on your view. Save this answer. Show activity on this post.

What is set_ rules in CodeIgniter?

Setting Validation RulesCodeIgniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data at the same time. To set validation rules you will use the set_rules() method: $this->form_validation->set_rules();

How many parameters are Set_rules function?

Setting Validation Rules To set validation rules you will use the set_rules() function: $this->form_validation->set_rules(); The above function takes three parameters as input: The field name - the exact name you've given the form field.


2 Answers

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!!

like image 76
MY_Mark Avatar answered Oct 24 '22 07:10

MY_Mark


you can set custom error like I mentioned below, no need to create custom function for this error message content change.

$validation = array(
    array(
        'field' => 'name',
        'label' => 'NAME', 
        'rules' => 'trim|required', 
        "errors" => array('required' => " Enter your %s. ")
    ),
);
$this->form_validation->set_rules($validation);
        if ($this->form_validation->run()) {}
like image 44
sankar Avatar answered Oct 24 '22 07:10

sankar