How to set up custom error messages for each form field in Codeigniter?

I am trying to customize a codeigniter form with various error messages.

set_message (typically msg) configures the message for the entire 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% s to the message line will not help in this case, since the messages should be completely different.

Perhaps 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 is there no smarter way to do this?

+5
source share
4 answers

I think a more sensible way would be to use the Codeigniter callback function (something similar below). The following works, but it may be possible to simplify it even more. If nothing else, this is the starting point.

( custom_required custom_check_length) ( , ).

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;
        }
    }

...

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;
        }
    }

set_message . , , callback _.

...

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

, -!

+7

, , .

$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()) {}
+4

. , , - set_message(). , , .

    $this->form_validation->set_rules('name', 'First Name', 'required|alpha')
    $this->form_validation->set_message('name', 'required', 'Enter your Name');
    $ this-> form_validation-> set_message ('name', 'alpha', 'Numbers in% s?');
    $ this-> form_validation-> run ();

    $ this-> form_validation-> set_rules ('second', 'Variables', 'required');
    $ this-> form_validation-> set_message ('second', 'required', 'Variables required');
    if ($ this-> form_validation-> run ()) {
        ...
    }

This verification approach works in all versions of Codeigniter.

0
source
$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]');**
-1
source

All Articles