首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zend formBuilder在复选框渲染时引发异常

Zend formBuilder在复选框渲染时引发异常
EN

Stack Overflow用户
提问于 2015-09-01 04:01:47
回答 1查看 168关注 0票数 1

我尝试在我的模型中使用带有注释的Zend Framework2 formBuilder,但在尝试呈现复选框时抛出异常:

模型属性注释:

代码语言:javascript
复制
/**
 * @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)

代码语言:javascript
复制
        <div id="Content">
            <?= $form->get('Content)->getLabel(); ?>
            <?= $this->formCheckbox($form->get('Content)); ?>
        </div>

当它尝试运行formCheckbox时,它会抛出

代码语言:javascript
复制
 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,但没有成功。

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2015-09-03 03:38:35

根据我的评论:

您必须将Type设置为Annotation,以便FormBuilder知道要创建哪个元素(默认值应为text)。

代码语言:javascript
复制
/**
 * @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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32319029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档