问题
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