squaretastic
squaretastic

Reputation: 123

In Symfony, include a partial form inside another form, with both based on the same entity

I understand that forms of related entities may be embedded within one another per the Symfony documentation at http://symfony.com/doc/current/book/forms.html#embedded-forms, and that collections of related entity forms may be embedded via the collection field type per the documentation at http://symfony.com/doc/current/cookbook/form/form_collections.html

I am looking for a way to "include" a partial form based on one entity inside the builder of another form type class, based on that same entity. For example, I may have ClientShortType and ClientLongType, and in ClientLongType, I want all of the fields from ClientShortType plus a few that I would add. Both ClientShortType and ClientLongType would use the Client entity.

I am very new to Symfony. Perhaps I am overlooking something, but I am unable to find a way to do this. Can someone please let me know how, or point me in the right direction if this is not the optimal way to go about this? Thank you!

Upvotes: 2

Views: 1067

Answers (1)

Cerad
Cerad

Reputation: 48865

All you should really have to do is to have the long type extend from the short type.

class ClientShortType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text');
    }
}
class ClientLongType extends ClientShortType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder,$options);

        $builder->add('zodiacSign', 'text');
    }
}

Upvotes: 2

Related Questions