Skip to content

[2.2+]hidden field not display in case of nested forms #6760

Closed
clotairer opened this Issue Jan 16, 2013 · 1 comment

2 participants

@clotairer

My case is :

A nested form with a contactType (with an entity Contact) with a sub type UserType (with an associated entity User)

  class ContactType extends AbstractType
  {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->addEventListener(FormEvents::PRE_BIND, function(FormEvent $event) use ($builder) {
            $data = $event->getData();
            if (isset($data['xxxxxxx'])) {
                $event->getForm()->add($builder->getFormFactory()->createNamed('user', new UserType(), null, array('required' => true, 'label' => ' ')));
                $event->getForm()->add($builder->getFormFactory()->createNamed('comment', 'textarea', null, array('required' => false, 'label' => 'Comment')));
            }
        });
    }
 }

A form UserType :

  class UserType extends AbstractType
  {
        $builder->add('firstname', 'text', array('label'  => 'Firstname', 'required' => true));
        $builder->add('lastname', 'text', array('label'  => 'Lastname', 'required' => true));
        $builder->add('timezone', 'hidden', array('data' => 'Europe/France',));
  }

When I display the contact form, the value of input hidden html tag is empty.
If I change the construction of the contact form without addEventListener like :

        $builder->add('comment', 'textarea');
        $builder->add('user', new UserType(false));

The attribute value is displayed.

I've not found others similar issues in this bug list. Can you help me and confirm me if it's a bug ?

Many thank's !

@jakzal jakzal added a commit to jakzal/symfony-standard that referenced this issue Dec 29, 2013
@jakzal jakzal Reproduced symfony/symfony#6760. 8ebfda7
@jakzal
Symfony member
jakzal commented Dec 29, 2013

@clotairer thanks for taking your time to report this issue.

I tried to reproduce it and it seems to be fixed already. See https://github.com/jakzal/symfony-standard/compare/issue6760 for steps I took.

In my example, here's what happens:

  • form is rendered with the submit button only, on a first request
  • after submitting the form, PRE_SUBMIT event (previously called PRE_BIND) is fired, your listener kicks in and as a result additional fields are rendered in the template
  • hidden field doesn't have a value since it wasn't submitted. Non submitted field values are set to empty by default.

Remember that PRE_SUBMIT event happens before the form value are submitted, so it won't be fired when you build the form on the first request (unless that's a submit request).

If you're still experiencing this issue, please reopen it and try reproducing on a standard edition fork (like I did).

@jakzal jakzal closed this Dec 29, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.