duanpo2037 2014-08-24 13:59
浏览 49
已采纳

Zend框架1.6使用Zend Filter Input验证POST请求

With Zend framework 1.6 I recive a POST request like this:

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"
  }
}

To Validate the 'nome' field I am using Zend Input Filter. This is my validator array:

    $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 ());
    }

It seems that the validation is not well formed becouse I recive the message:

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

In my opinion there is an error in my $validators array, but I can't find it.

  • 写回答

1条回答 默认 最新

  • dongliao3450 2014-08-24 18:45
    关注

    Your problem is, you use an array notation in your html form.

    You may have something like this in your html:

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

    This is called array notation in form elements.

    You can get this array in ZF by calling:

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

    But there are things happening, I dont understand..

    If using the next approach, everything works just fine...

    $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());
    }
    

    Hope it helps.

    Read more about Zend_Validate_Input and the metacommands here: http://framework.zend.com/manual/1.6/de/zend.filter.input.html#zend.filter.input.metacommands.presence

    And more about the available validator classes here: http://framework.zend.com/manual/1.6/de/zend.validate.set.html

    And maybe think about using a Zend_Form to generate an Object that has all the validators, filters and elements you need. You dont need to use this form to render an html output. But use it for validating and filtering is much simpler than doing it manually for all kind of forms.

    Have fun and good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化