[body listener] how are embedded forms (and collections) handled? #388

Closed
patkar opened this Issue Mar 6, 2013 · 3 comments

Comments

Projects
None yet
3 participants

patkar commented Mar 6, 2013

In one of my forms I'm using a collection of embedded forms

$builder->add('addresses', 'collection', array(
                    'type' => new AddressType(),
                    'allow_add' => true,
                    'allow_delete' => true,
                    'by_reference' => false,
                    'description' => 'Array of addresses',
            ));

How the json must look like, that the body listener recognizes the addresses array and I can bind the request correctly to my form?
This json would be logic to me, but it doesn't work.

{
    "user": {
        "username": "foo",
        "email": "foo@example.org",
        "plainPassword": "hahaha",
        "firstname": "Foo",
        "lastname": "bar",
        "language": "en",
        "addresses": [{
            "type": "invoice",
            "street1": "Broadway 1",
            "zip": "12345",
            "city": "New York",
            "country": "US"
        }]
    }
}

My controller, which handles the post request is very basic.

/**
 * @Rest\Post("/users")
 * @Rest\View
 */
public function newAction()
{
    return $this->processForm(new User(), new RegistrationUserType());
}

private function processForm(User $user, FormTypeInterface $type)
{
    $userManager = $this->get('fos_user.user_manager');
    $statusCode = (null === $user->getId()) ? 201 : 204;

    $form = $this->createForm($type, $user);
    $form->bind($this->getRequest());

    if ($form->isValid()) {
        $user = $form->getData();
        return $user;

        $userManager->updateUser($user);

        $response = new Response();
        $response->setStatusCode($statusCode);
        $response->headers
            ->set('Location', $this
               ->generateUrl('gps_api_user_get', array(
                       'id' => $user->getId()
               ), true));

        return $response;
    }

    return $form;
}
Contributor

borisguery commented Mar 14, 2013

it should work out of the box, I use such similar feature successfully. Maybe you should clarify what "doesn't work". However, your question would better fit on the mailing list, irc, or SO to avoid bloating the issues list :)

Owner

lsmith77 commented May 7, 2013

ping

patkar commented May 8, 2013

close, 'cause not a bug or issue. Sorry for bloating the issue list.

patkar closed this May 8, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment