Inherit form or add type to each form
Asked Answered
D

2

10

I am searching for an easy way to add a bundle of fields to each form.

I have found a way to extend the AbstractType and use the buildForm method to add more fields.
When creating the form I give the name of my new type (How to Create a Custom Form Field Type).

In my opinion it is an easy way, but it is restricted to one type per form.

Is there a better way to achieve anything like that?
I have read the cookbook of symfony, but I have only found stuff how to extend an existing form not how to create an own form "template" with my fields.

Dextro answered 14/3, 2014 at 19:32 Comment(0)
S
23

Have you tried using inheritance?

This is really simple, first you have to define a form type:

# file: Your\Bundle\Form\BaseType.php
<?php

namespace Your\Bundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class BaseType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text');

        $builder->add('add', 'submit');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Your\Bundle\Entity\YourEntity',
        ));
    }

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

Then you can extend this form type:

# file: Your\Bundle\Form\ExtendType.php
<?php

namespace Your\Bundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ExtendType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        # you can also remove an element from the parent form type
        # $builder->remove('some_field');

        $builder->add('number', 'integer');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Your\Bundle\Entity\YourEntity',
        ));
    }

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

The BaseType will display a name field and an add submit button. The ExtendType will display a name field, an add submit button and a number field.

Sterrett answered 14/3, 2014 at 22:27 Comment(7)
If you extended BaseType with ExtendType as you are now then you would overwrite the parent buildForm with the child. To extend it properly you would need to include parent::buildForm($builder, $options) at the start of the child buildForm.Misname
You did it in the wrong one. Abstract Type has an empty buildForm so rebuilding that in BaseType would be pointless. You need to use the parent::buildForm(etc) in the ExtendType so that it also uses the buildForm in the BaseType.Misname
Thanks a lot! That's fixed.Sterrett
When I asked I wasn't really sure about the form system and its behavior. Seems to be easier than I thought/understand.Dextro
But it seems it isn't possible to add an extension to the base type via AbstractTypeExtension.Dextro
I'M not sure that I understand your question. AbstractTypeExtension is a class from Symfony2, you want to extend it?Sterrett
Oh didn't seen that you already answered. I meant I want to register an extension for the BaseType which I extend.Dextro
F
11

You can do this with the getParent() function.

<?php

namespace Your\Bundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;


class ChildType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // $builder->remove('field');
        // $builder->add('field);
    }

    public function getParent()
    {
        return ParentType::class;
    }
}
Flats answered 7/9, 2017 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.