How to process nested json with FOSRestBundle and symfony forms How to process nested json with FOSRestBundle and symfony forms symfony symfony

How to process nested json with FOSRestBundle and symfony forms


This is a pretty good question. One solution that comes to mind is making an unmapped form and binding data manually using a form event, for example:

public function buildForm(FormBuilderInterface $builder, array $options){    // Make a "nested" address form to resemble JSON structure    $addressBuilder = $builder->create('address', 'form')        ->add('place')        ->add('street');    $builder->add('username');    $builder->add('email', 'email');    $builder->add('password', 'password');    // add that subform to main form, and make it unmapped    // this will tell symfony not to bind any data to user object    $builder->add($addressBuilder, null, ['mapped' => false]);    // Once form is submitted, get data from address form    // and set it on user object manually    $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {        $user = $event->getData();        $addressData = $event->getForm()->get('address')->getData();        $user->setStreet($addressData['street']);        $user->setPlace($addressData['place']);    })}