【问题标题】:Zend framework 1.6 validate POST request with Zend Filter InputZend 框架 1.6 使用 Zend 过滤器输入验证 POST 请求
【发布时间】:2014-08-24 13:59:00
【问题描述】:

使用 Zend 框架 1.6,我收到这样的 POST 请求:

array(2) {
  ["checkins"] => array(18) {
    ["nome"] => string(6) "MYNAME" //the field is correctly filled
    ["pec"] => string(0) ""
    ["sito_web"] => string(0) ""
    ["note"] => string(0) ""
  }
  ["users"] => array(19) {
    ["email"] => string(29) "email@gmail.com"
    ["u_id"] => string(1) "1"
  }
}

为了验证“nome”字段,我正在使用 Zend 输入过滤器。这是我的验证器数组:

    $validators = array (
                'nome' => array (
                        'presence' => 'required'
    ));
    $params = $this->getRequest ()->getPost ();

    $input = new Zend_Filter_Input ( array (), $validators,$params );

    if (! $input->isValid ()) {
        print_r($input->getMessages ());
    }

似乎验证格式不正确,因为我收到了消息:

Field 'nome' is required by rule 'nome', but the field is missing

在我看来,我的 $validators 数组中有一个错误,但我找不到它。

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    您的问题是,您在 html 表单中使用了数组表示法。

    您的 html 中可能有这样的内容:

    <form ...>
    ...
    <input type="text" name="checkins[nome]" ... /> 
    ...
    </form>
    

    这在表单元素中称为数组表示法。

    在 ZF 中可以通过调用来获取这个数组:

    $request = $this->getRequest();
    
        if($request->isPost()) {
    
            $post = $request->getPost();
    
            $validators = array (
                //nome has to be present and not empty
                'checkins' => array(
                    'nome' => array(
                        'NotEmpty',
                        'presence' => 'required'
                    ),
                    //if pec is present, it has to be not empty
                    'pec' => array(
                        'NotEmpty'
                    )
                )
            );
    
            //validate the post array
            $input = new Zend_Filter_Input( array(), $validators, $post);
    
            if (!$input->isValid()) {
                Zend_Debug::dump($input->getMessages());
            }
    
            Zend_Debug::dump($post);
        }
    

    但有些事情正在发生,我不明白..

    如果使用下一种方法,一切正常...

    $request = $this->getRequest();
    
    $checkins = $request->getPost('checkins');
    
    $validators = array (
        //nome has to be present and not empty
        'nome' => array(
            'NotEmpty',
            'presence' => 'required'
        ),
        //if pec is present, it has to be not empty
        'pec' => array(
            'NotEmpty'
        )
    );
    
    //validate the checkins array instead of the whole array
    $checkinsFilter = new Zend_Filter_Input( array(), $validators, $checkins);
    
    if (!$checkinsFilter->isValid()) {
        Zend_Debug::dump($checkinsFilter->getMessages());
    }
    

    希望对你有帮助。

    在此处阅读有关 Zend_Validate_Input 和元命令的更多信息: http://framework.zend.com/manual/1.6/de/zend.filter.input.html#zend.filter.input.metacommands.presence

    还有更多关于可用验证器类的信息: http://framework.zend.com/manual/1.6/de/zend.validate.set.html

    也许考虑使用 Zend_Form 来生成一个包含所有你需要的验证器、过滤器和元素的对象。您不需要使用此表单来呈现 html 输出。但是使用它来验证和过滤比手动对所有类型的表单进行验证和过滤要简单得多。

    玩得开心,祝你好运!

    【讨论】:

    • 我需要所有 POST 请求参数,而不仅仅是“名称”。
    • 嗨,我已经更新了我的答案。也许这有帮助。您在 html 表单或 POST 请求正文中使用数组表示法。你可以看看 body,然后你会看到 POST 是如何工作的。它只是键=值对的组合,由 & 组合而成。数组表示法是通过在后参数中添加括号来完成的,键名在 checkins[nome].
    • 您的第二个解决方案是最好的,但不幸的是不起作用。我正在尝试一些解决方法,但没有任何效果。很奇怪没有人有这个问题!!
    • 我也试过了,但不幸的是它没有像预期的那样工作......您需要单独验证每个
      。让它工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 2010-10-02
    • 2017-11-09
    • 1970-01-01
    相关资源
    最近更新 更多