How can I setup custom error messages for each for

2019-01-19 22:33发布

问题:

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:

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



回答2:

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


回答3:

You were almost there with your initial thought. All you needed was to execute validation after each group of set_message() changes. I find this approach much easier and faster than callback functions, unless you are going to use this exact customisation in many places.


    $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 validation approach works on all versions of Codeigniter.



回答4:

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