Symfony2 Forms: Hidden entity type part 2

August 1st 2013

(please note this article is not up to date, although the content is helpful, please see the repository readme)

A few months ago I wrote a tutorial on using a data transformer to add entities as valid hidden fields in a Symfony2 form.  A few of the comments left by readers either noted a gist that showed an installable hidden entity type (that does not exist any more) or mentioned that it was a lot of code to write in order to gain the functionality. Well, good news for those readers (and hopefully you too), I have finally gotten around to putting the contents of the tutorial into a component that you can install via composer into any of your Symfony2 projects.  This component will allow you to render any entity that has an id as a hidden field in a Symfony2 form! You can see the code in it's GitHub repository here: https://github.com/LRotherfield/Form There is a readme file on the repository but I will go over the installation and usage here too. First lets install it, this is simply done by adding the "lrotherfield/form" package to your requirements in composer.json:

"require": {
        "lrotherfield/form": "1.1.x-dev",
}

Then update your vendors: php composer.phar update Next we need to register the hidden_entity form type as a service and tag it as a form.type.  This can be done in any loaded config yaml file like app/config.yml or any bundle config/services.yml file.

services:
    lrotherfield.form.type.hidden_entity:
        class: Lrotherfield\Component\Form\Type\HiddenEntityType
        arguments:
            - @doctrine.orm.entity_manager
        tags:
            - { name: form.type, alias: hidden_entity }

Finally we can now use the hidden_entity type as a form builder type argument.  The only additional option we need to define is entity_repository.  This must be the fully qualified namespace or doctrine logical name of the entity that you want to transform into a hidden field.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        //...
        ->add('example', 'hidden_entity', array(
                "entity_repository" => "Lrotherfield\\Bundle\\ExampleBundle\\Entity\\ExampleEntity"
            ));
    ;
}

I hope this helps anyone after rendering entities as hidden fields in Symfony2 forms.  If you have any questions, put them in a comment using the form below.

Update

I have released a new tag of the hidden entity component that adds two changes:

1) As pointed out in the comments below, entity_repository is not really a good key for the entity class so I have changed the key to class. 2) I have added the ability to provide a custom entity transformer if desired. The new implementation of the component with the updates should look something like:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        //...
        ->add('example', 'hidden_entity', array(
                "class" => "Lrotherfield\\Bundle\\ExampleBundle\\Entity\\ExampleEntity",
        //Optional entity transformer
        "transformer" => "YourNamespace\Form\DataTransformer\YourTransformer"
            ));
    ;
}
Luke Rotherfield

Symfony2 Developer in CT USA. Luke is a Symfony2 wizard and has written some sweet libraries of his own. Luke loves Jesus, his gorgeous wife and his two beautiful daughters :)