问题是将表单数据从客户端发送到Zend Controller之一。我想使用Form的isValid()方法验证表单。但是它总是返回false。我认为我无法将正确的格式数据提供给$ formData-> setData()。详细信息如下。
.....
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可以接受的正确格式是什么?如何获得该格式?
答案 0 :(得分:1)
将行修改为:
$data = $this->params()->fromPost();
收件人
$data = $this->params()->fromPost()->toArray();
或执行以下操作:
$form->setData($this->getRequest()->getPost());