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

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

问题描述



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

p>

我需要:

  $ this-> form_validation-> set_rules ('name','First Name','required'); 
$ this-> form_validation-> set_message('name','required','输入您的姓名');
$ this-> form_validation-> set_rules('second','Variables','required');
$ this-> form_validation-> set_message('second','required',
'需要变量');

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



可能我可以这样做:



controller


b $ b

  $ 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')){
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'变量是必需的;
break;
case'< p> short< / p>':
echo'min length 3';
break;
case'< p> long< / p>':
echo'min length 5';
break;

}

但是没有更聪明的方法吗?

解决方案

我认为一个更聪明的方法是使用Codeigniter的回调功能(类似于下面的内容)。以下工作,但它可能是流水线甚至更多。



创建两个回调函数(我已命名这些 custom_required custom_check_length

 私有函数_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','变量是必需的');
return(trim($ str)=='')? FALSE:TRUE;
break;
}
}

和...

 私有函数_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'。
return FALSE;
} elseif(strlen($ str)> $ max){
$ this-> form_validation-> set_message('custom_check_length','Max length'。
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]');

我希望以上帮助在某种程度上!


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天全站免登陆