我尝试在我的模型中使用带有注释的Zend Framework2 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 =>‘复选框。
我甚至尝试过将'checkbox‘更改为\Zend\Form\Element\Checkbox和Zend\Form\Element\Checkbox,但没有成功。
任何帮助都是非常感谢的。
发布于 2015-09-03 03:38:35
根据我的评论:
您必须将Type设置为Annotation,以便FormBuilder知道要创建哪个元素(默认值应为text)。
/**
* @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;https://stackoverflow.com/questions/32319029
复制相似问题