如何为 Codeigniter 中的每个表单字段设置自定义错误消息? [英] How can I setup custom error messages for each form field in Codeigniter?

查看:20
本文介绍了如何为 Codeigniter 中的每个表单字段设置自定义错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个带有不同错误消息的 codeigniter 表单.

set_message(rule, msg) 正在为整个表单设置一条消息.

我需要:

$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','变量是必需的');

在这种情况下,将 %s 添加到消息字符串中没有帮助,因为消息必须完全不同.

也许我可以这样做:

控制器

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

查看

switch(form_error('name')) {情况'

必需

':echo '输入你的名字';休息;情况<p>短</p>":echo '最小长度 6';休息;情况'<p>long</p>':echo '最小长度 12';休息;}开关(form_error('第二')){情况'

必需

':echo '变量是必需的';休息;情况<p>短</p>":echo '最小长度 3';休息;情况'<p>long</p>':echo '最小长度 5';休息;}

但是没有更聪明的方法来做到这一点吗?

解决方案

我认为更聪明的方法是使用 Codeigniter 的回调功能(类似于下面的内容).以下工作,但有可能进一步简化它.如果不出意外,这是一个起点.

创建两个回调函数(我将它们命名为 custom_requiredcustom_check_length)并将它们放在控制器的底部(或您认为需要的任何地方).

私有函数_custom_required($str, $func) {开关($func){案例名称":$this->form_validation->set_message('custom_required', '输入你的名字');返回 (trim($str) == '') ?假:真;休息;案例第二":$this->form_validation->set_message('custom_required', '变量是必需的');返回 (trim($str) == '') ?假:真;休息;}}

还有……

私有函数_custom_check_length($str, $params) {$val = expand(',', $params);$min = $val[0];$max = $val[1];if(strlen($str) <= $max && strlen($str) >= $min) {返回真;} elseif(strlen($str) < $min) {$this->form_validation->set_message('custom_check_length', 'Min length' . $min);返回假;} elseif(strlen($str) > $max) {$this->form_validation->set_message('custom_check_length', 'Max length' . $max);返回假;}}

这两个函数负责表单验证的 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]');

希望以上内容对您有所帮助!!

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?

解决方案

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

这篇关于如何为 Codeigniter 中的每个表单字段设置自定义错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆