$ form-> setData()以哪种格式在控制器中接受表单数据 [英] In what format does $form->setData() accepts the form data in controller

查看:109
本文介绍了$ form-> setData()以哪种格式在控制器中接受表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是将表单数据从客户端发送到Zend Controller之一.我想使用Form的isValid()方法验证表单.但是它总是返回false.我认为我无法将正确的格式数据提供给$ formData-> setData().详细信息如下.

The problem is one of sending the form data from client to the Zend Controller. I want to validate the form using isValid() method of Form. But it always returns false. I think I am not able to give right format data to $formData->setData(). The details are as given below.

 .....
var fData = $('#responsibleAddressForm').serializeArray();
$.ajax({ 
       url:        '/vvt/controller      /storeResponsibleAddress', 
       data:    fData,
       type:       'POST',  
       dataType:   'json', 
       async:      true, 
       success: function(controller) { 
                  alert("in success!");
            //$("#wpno-vvt-contacts").html(contactsHtml);

                    }, 
       error : function(xhr, textStatus, errorThrown) { 
                       alert('Ajax request failed.'); 
                    } 
                 }); 
.....
But when I try to validate the form data on controller, as follows:

....

    if($this->getRequest()->isPost())
    {
        // Fill in the form with POST data
        $form    = new ControllerForm($this->entityManager);
        $view->setTerminal($request->isXmlHttpRequest());

        $data = $this->params()->fromPost();

        $form->setData($data);
        // Validate form
        if($form->isValid()) {
 .....

isValid始终为false.我假设它返回false,因为我给出的是url编码的字符串,而不是setData接受的正确格式.现在,如果这是原因,那么setData可以接受的正确格式是什么,我该如何获得呢?

isValid is always false. I assume I it returns false because I am giving url encoded string which is not the right format that setData accepts. Now, if this is the reason then what is the right format which is acceptable to setData and how do I get that?

推荐答案

将声明的行修改为:

$data = $this->params()->fromPost();

收件人

$data = $this->params()->fromPost()->toArray();

或执行以下操作:

$form->setData($this->getRequest()->getPost());

这篇关于$ form-> setData()以哪种格式在控制器中接受表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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