How to process nested json with FOSRestBundle and symfony forms

ぃ、小莉子 提交于 2019-12-07 15:30:48

问题


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

{
  "user": {
    "id": 999,
    "username": "xxxx",
    "email": "xxxx@example.org",
    "address": {
      "street": "13th avenue",
      "place": 12
    }
  }
}

instead of flat one

{
  "user": {
    "id": 999,
    "username": "xxxx",
    "email": "xxxx@example.org",
    "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:


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']);
    })
}


来源:https://stackoverflow.com/questions/34690274/how-to-process-nested-json-with-fosrestbundle-and-symfony-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!