【发布时间】:2014-06-11 12:09:38
【问题描述】:
我正在使用 Zend Framework 2.3.1 ( PHP 5.4.21 ) 我正在尝试使用注释构建表单,我使用的代码很简单:
控制器:
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Entity\Test;
use DoctrineORMModule\Form\Annotation\AnnotationBuilder;
// ...
$entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
/* @var $entityManager \Doctrine\ORM\EntityManager */
$test = new Test();
$builder = new AnnotationBuilder($entityManager);
// for debugging purposes
$spec = $builder->getFormSpecification($test);
\Zend\Debug\Debug::dump($spec);
Application\Entity\Test 看起来像这样:
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
* @Annotation\Name("this-works")
* @ORM\Entity
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
*/
class Test{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
* @Annotation\Attributes({"type":"hidden"})
*/
protected $id;
/**
*
* @ORM\Column(type="string")
* @Annotation\Filter({"name":"StringTrim"})
* @Annotation\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
* @Annotation\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9_-]{0,24}$/"}})
* @Annotation\Attributes({"type":"text"})
* @Annotation\Options({"label":"Username:"})
* @Annotation\AllowEmpty(true)
*/
protected $fullName;
}
我得到的结果是:
object(ArrayObject)#338 (1) {
["storage":"ArrayObject":private] => array(6) {
["name"] => string(4) "this-works"
["attributes"] => array(0) {
}
["elements"] => array(0) {
}
["fieldsets"] => array(0) {
}
["hydrator"] => string(35) "Zend\Stdlib\Hydrator\ObjectProperty"
["input_filter"] => object(ArrayObject)#339 (1) {
["storage":"ArrayObject":private] => array(0) {
}
}
}
}
表单名称注释正在工作,但所有字段都被排除在外。
我试图调试它,我发现如果我从(注意删除回调)更改 Zend/Form/Annotation/AnnotationBuilder.php 函数 checkForExclude
protected function checkForExclude($annotations)
{
$results = $this->getEventManager()->trigger('checkForExclude', $this, array(
'annotations' => $annotations,
), function ($r) {
return (true === $r);
});
return (bool) $results->last();
}
到
protected function checkForExclude($annotations)
{
$results = $this->getEventManager()->trigger('checkForExclude', $this, array(
'annotations' => $annotations,
));
return (bool) $results->last();
}
它工作正常(fullName 字段在表单声明中)。
我做错了什么还是只是 ZF2 中的错误? 现在想弄清楚这几天,但我没有想法。
【问题讨论】:
标签: php forms annotations zend-framework2