In what format does $form->setData() accepts the form data in controller

廉价感情. 提交于 2019-12-13 03:07:47

问题


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 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?


回答1:


Modify the line stated as:

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

To

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

Or do this:

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


来源:https://stackoverflow.com/questions/55770500/in-what-format-does-form-setdata-accepts-the-form-data-in-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!