我想验证我通过Ajax提交的表单,但这对我来说不起作用。请帮助解决这个问题。
在我的注释生成器中,我使用了:
<?php
namespace ApplicationModel;
use ZendFormAnnotation;
/**
* @AnnotationName("sejour")
* @AnnotationHydrator("ZendStdlibHydratorObjectProperty")
*/
class Sejour {
public $id;
/**
* @AnnotationValidator({"name":"StringLength", "options":{"min":1, "max":3}})
*/
public $titre;
public $agenceId;
public $agenceLibelle;
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->titre = (isset($data['titre'])) ? $data['titre'] : null;
$this->agenceId = (isset($data['agenceId'])) ? $data['agenceId'] : null;
$this->agenceLibelle = (isset($data['agenceLibelle'])) ? $data['agenceLibelle'] : null;
}
}
在我的控制器文件中:
$values = json_decode(file_get_contents('php://input'), true);
$builder = new AnnotationBuilder();
$entity = new Sejour();
$form = $builder->createForm($entity);
$form->bind($entity);
$form->setData($values);
if ( $form->isValid()) {
/* save datas in base */
}else{
$errors = $form->getMessages();
print_r($errors);
}
每次我都没有错误
请帮我
感谢
这是一个很好的例子。
http://samsonasik.wordpress.com/2012/10/11/zend-framework-2-using-zend-form-and-ajax/
试试这个
根据您的Zend Framework 2版本,您可能会发现preferFormInputFilter标志在zf2 2.2.4中更改了其默认值。
该标志调节表单的行为,决定在验证过程中,表单应该使用自己的输入过滤器(您事先提供的)还是每个元素提供的过滤器。
使用AnnotationBuilder创建的表单的默认值是将标志设置为true,因此,由于您没有在实际设置筛选器的位置粘贴任何代码,因此默认筛选器为none。
您可能想要为您添加的元素启用默认元素过滤器,方法是添加:
$form->setPreferFormInputFilter(FALSE);
在您创建表单之后。
另一个选项自然是为表单提供实际的输入过滤器。