gastoncs
gastoncs

Reputation: 117

Pass entity in a hidden form field type

I have been trying to setup a hidden custom field type and a transformer like in this example: https://gist.github.com/bjo3rnf/4061232

What I am trying to accomplish is to pass an entity trough a hidden element, that entity should map the form´s entity but for some reason when it get to the controller it get empty.

Can some one help me on how can I pass an entity trough a form?

Thank you

Upvotes: 1

Views: 2012

Answers (1)

LorenzSchaef
LorenzSchaef

Reputation: 1543

The solution with the custom type and transformer is probably better, but if you just want a quick hack, you can set your field up as an entity type in the formbuilder

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('yourField', 'entity', array('class' => 'AppBundle\Entity\YourEntity', 'read_only' => true));
    }

and then do something like this in your template:

<input type="hidden" id="{{ form.yourField.vars.id }}" 
    name="{{ form.yourField.vars.full_name }}" 
    value="{{ form.yourField.vars.value }}" />
{% do form.yourField.setRendered %}

Note that this has to be at the top of your form, before you invoke form_widget()

Upvotes: 1

Related Questions