简体   繁体   English

Codeigniter表单验证回调函数不起作用

[英]Codeigniter form validation callback function not working

I've written a simple callback function which isn't working. 我已经编写了一个无法正常工作的简单回调函数。 My other callbacks (which are in the same library file) work fine so I guess the problem has to do with my code. 我的其他回调(在同一个库文件中)可以正常工作,因此我想问题与我的代码有关。

The parameter passed in the callback function takes the form of a chunk of PHP which is eval()'ed to form part of an 'if()' statement in the function itself. 回调函数中传递的参数采用PHP块的形式,该PHP块通过eval()构成函数本身中“ if()”语句的一部分。

Here's what's in the controller: 这是控制器中的内容:

$this->form_validation->set_rules('rating', 'Rating','required');
$condition = $this->input->post('rating')  . " != 'Excellent'";
$this->form_validation->set_rules('details', 'Details', 'required_conditional[' . htmlentities($condition) .']');

And here's the callback function itself: 这是回调函数本身:

function required_conditional($str, $condition)
{
    if (eval(html_entity_decode($condition))) {
        if ($str == '') {
            $this->set_message('required_conditional', 'The %s field is required');
            return FALSE;
        }
        else {
            return TRUE;
        }
    }
}

Any ideas why it's not working anyone? 有什么想法为什么它不起作用吗?

Thanks, Matt 谢谢,马特

It's because eval evaluates statements, not expressions. 这是因为eval评估语句,而不是表达式。 This will give you a parse error: 这将给您一个解析错误:

$test = "1 > 0";
if (eval($test)) { echo "echo!"; }

And this will work as you expect it to: 这将按您期望的那样工作:

$test = "return 1 > 0;";
if (eval($test)) { echo "echo!"; }

您不应该使用“ callback_<function name> ”吗?

是的,请使用正确的语法来调用表单验证回调以使用“ callback_”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM