在symfony2中创建自定义表单类型:无法覆盖buildView() [英] Creating a custom form type in symfony2: can't overwrite buildView()

查看:36
本文介绍了在symfony2中创建自定义表单类型:无法覆盖buildView()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在symfony2中创建一个自定义表单类型.但是每次我尝试覆盖buildForm()方法时,都会出现此错误:

I am creating a custom form type in symfony2. But every time I try to overwrite the buildForm() method I get this error:

致命错误:SeduceMe \ SiteBundle \ Form \ Type \ UniFormTextType :: buildView()的声明必须与/Users/alexander/Projekte/SeduceMe/中的Symfony \ Component \ Form \ FormTypeInterface :: buildView()的声明兼容第33行上的serversymfony204/src/SeduceMe/SiteBundle/Form/Type/UniFormTextType.php

Fatal error: Declaration of SeduceMe\SiteBundle\Form\Type\UniFormTextType::buildView() must be compatible with that of Symfony\Component\Form\FormTypeInterface::buildView() in /Users/alexander/Projekte/SeduceMe/serversymfony204/src/SeduceMe/SiteBundle/Form/Type/UniFormTextType.php on line 33

我当然知道这意味着什么.我什至从提到的界面复制了方法签名.还是一样.这是我的课程:

Of course I understand what this means. I even copied the method signature from the mentioned interface. Still the same. This is my class:

namespace SeduceMe\SiteBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class UniFormTextType extends AbstractType 
{
    public function getDefaultOptions(array $options)
    {
        return array('placeholder' => null);
    }

    public function getParent(array $options)
    {
        return 'text';
    }

    public function getName()
    {
        return 'UniFormText';
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->setAttribute('placeholder', $options['placeholder']);
    }

    public function buildView(FormView $view, FormInterface $form)
    {
        $view->set('placeholder', $form->getAttribute('placeholder'));
    }
}

推荐答案

需要为FormView和FormInterface添加use语句.

Need to add use statements for FormView and FormInterface.

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\CallbackValidator;
use Symfony\Component\Form\FormValidatorInterface;

这篇关于在symfony2中创建自定义表单类型:无法覆盖buildView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆