Codeigniter form_error()仅显示一个错误 [英] Codeigniter form_error() displays only one error

查看:61
本文介绍了Codeigniter form_error()仅显示一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行CI表单验证,并使用form_error()分别显示每个字段的错误.但是,问题在于form_error()函数仅返回第一个验证错误

I'm running CI form validation and using form_error() to display errors individually for each field. The problem, though, is that the form_error() function only returns the first validation error

示例字段:

<input type="text" name="value" value="short">

验证脚本:

$validationRules = "min_length[6]|numeric";
$this->form_validation->set_rules(
    'value',
    'lang:mod_the_field',
    $validationRules
);
if($this->form_validation->run() === FALSE){
    echo form_error('value');
}

以上输出:< p> Value字段的长度必须至少为5个字符.< \/p>

尽管该字段也无法通过数值验证.

Although the field also fails validation as a numeric value.

我似乎找不到关于此的文档.我是否缺少一个简单的步骤来获取单个字段输出的所有验证错误?有没有比多次运行验证更好的解决方案了,每个规则一次?

I cannot seem to find documentation on this. Is there a simple step I am missing to get all validation errors for a single field output? Is there a better solution than running the validation multiple times, once for each rule?

清楚一点,我在此特定示例中寻找的输出是:

To be clear, the output I'm looking for in this particular example is:

<p>The Value field must contain only numbers.</p>
<p>The Value field must be at least 5 characters in length.</p>

一种解决方案是为每个规则运行一次验证并汇总输出,但是我试图找出是否有一种方法可以在CI中立即使用?

One solution is to run the validation once for each rule and aggregate the output, but I'm trying to find out if there is a way to do this out of the box in CI?

解决方案:

我检查了Form_validation库,发现Samutz的注释正确.该脚本将中断,并在出现第一个验证错误后返回.

I checked the Form_validation library and found that Samutz' comment is correct. The script breaks out and returns after the first validation error.

我没有扩展任何类,而是更改了代码以遍历每个规则并连接错误字符串,如下所示:

rather than extending any classes, I changed my code to iterate over each rule and concatenate the error string like so:

$validationRules = "min_length[6]|numeric";
$validationRules = explode('|', $validationRules);

$errors = '';
foreach($validationRules as $rule){
    $this->form_validation->set_rules(
        'value',
        'lang:mod_the_field',
        $rule
    );

    if($this->form_validation->run() === FALSE){
        $errors .= form_error('value');
    }
}
$errors = ($errors) ? $errors : FALSE;

echo $errors;

推荐答案

第一个错误

第二次错误

这篇关于Codeigniter form_error()仅显示一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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