我正在尝试在我的模型中使用带有注释的Zend Framework 2 formBuilder,但在尝试渲染复选框时它会抛出异常:
模型属性注释:
/**
* @var boolean $Content
*
* @ORM\Column(name="Content", type="boolean", nullable=false)
* @Annotation\Attributes({"type":"checkbox"})
* @Annotation\Options({"label":"Value:"})
* @Annotation\AllowEmpty({"true"})
* @Annotation\Filter({"name":"Boolean"})
*/
protected $Content = true;
来自表单模板的HTML(phtml)
<div id="Content">
<?= $form->get('Content)->getLabel(); ?>
<?= $this->formCheckbox($form->get('Content)); ?>
</div>
当它试图运行formCheckbox时,它正在抛出
PHP Fatal error: Uncaught exception 'Zend\\Form\\Exception\\InvalidArgumentException' with message 'Zend\\Form\\View\\Helper\\FormCheckbox::render requires that the element is of type Zend\\Form\\Element\\Checkbox' in /media/finaoweb/doctrine-test/vendor/zendframework/zend-form/src/View/Helper/FormCheckbox.php:29
Stack trace:
#0 /media/finaoweb/doctrine-test/vendor/zendframework/zend-form/src/View/Helper/FormInput.php(101): Zend\\Form\\View\\Helper\\FormCheckbox->render(Object(Zend\\Form\\Element))
#1 [internal function]: Zend\\Form\\View\\Helper\\FormInput->__invoke(Object(Zend\\Form\\Element))
#2 /media/finaoweb/doctrine-test/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(393): call_user_func_array(Object(Zend\\Form\\View\\Helper\\FormCheckbox), Array)
#3 /media/finaoweb/doctrine-test/module/Application/view/application/itemoption/edititemoption.phtml(34): Zend\\View\\Renderer\\PhpRenderer->__call('formCheckbox', Array)
在formCheckbox()调用之前查看堆栈跟踪,我可以看到元素属性,包括'type =&gt; “复选框”'
我甚至尝试将'复选框'更改为\ Zend \ Form \ Element \ Checkbox和Zend \\ Form \\ Element \\ Checkbox,但没有运气。
感谢任何帮助。
答案 0 :(得分:1)
根据我的评论:
您必须将Type设置为Annotation,以便FormBuilder知道要创建哪个元素(默认应该是文本)。
/**
* @var boolean $Content
*
* @ORM\Column(name="Content", type="boolean", nullable=false)
*
* @Annotation\Type("Zend\Form\Element\Checkbox")
* @Annotation\Attributes({"type":"checkbox"})
* @Annotation\Options({"label":"Value:"})
* @Annotation\AllowEmpty({"true"})
* @Annotation\Filter({"name":"Boolean"})
*/
protected $Content = true;