How to re-use forms with some fields left out in Symfony2

梦想与她 提交于 2019-12-05 17:38:29

I think the best solution is to create a base abstract Type and let your Types extend it. Here's a short example:

namespace Acme\Bundle\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;

abstract FooBaseType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        // Add all fields common for your other types.
    }
}

Now you can just extend it an include the missing fields

namespace Acme\Bundle\DemoBundle\Form\Type;

class ExampleType extends FooBaseType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        parent::buildForm($builder, $options);
        // Your missing fields
    }
}

see this github pull request: https://github.com/symfony/symfony-docs/pull/765

it seems to be exactly what you are looking for.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!