4

By nested json I mean something that keeps address data in its own "address" array:

{
  "user": {
    "id": 999,
    "username": "xxxx",
    "email": "[email protected]",
    "address": {
      "street": "13th avenue",
      "place": 12
    }
  }
}

instead of flat one

{
  "user": {
    "id": 999,
    "username": "xxxx",
    "email": "[email protected]",
    "street": "13th avenue",
    "place": 12
  }
}

Flat one is processed fine there using User entity and it's properties: "id", "username" and "email". It is nicely validated using symfony form feature:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('username');
    $builder->add('email', 'email');
    $builder->add('password', 'password');
    $builder->add('street');
    $builder->add('place');
}

I want to have both "street" and "place" as properties in User entity, to store it all in one user table in the database, using doctrine. But the json I get comes from third party, so I can not modify it.

Is there any way of constructing the form so it can validate the json with "address" field correctly, still being able to keep all the user data in one table?

1 Answer 1

3

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']);
    })
}
3
  • Thanks, seems like something that should work, but I get 'You cannot add children to a simple form. Maybe you should set the option "compound" to true?' with it. I tried to set compound to true in setDefaultOptions method, but to no avail. Jan 10, 2016 at 6:11
  • 1
    @BartBiczBorzy Ok, my bad, address type needs a type, as it seems that it's 'text' by default. Add that second argument to $builder->create method Jan 10, 2016 at 6:52
  • Yes it helped indeed. But form still couldn't be created because it expected 'address' setters and getters. I moved ['mapped' => false] part to $builder->create so I have now: $addressBuilder = $builder->create('address', 'form', ['mapped' => false]) $builder->add($addressBuilder); and it works now. Jan 13, 2016 at 6:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.