symfony2 custom input name

119 Views Asked by At

I'm building a REST API using Symfony 2.3 & FOSRestBundle.

Do anyone know how to customize POST parameter keys, so that, for instance: "company_mybundle_user[firstName]={value}" becomes

"firstName={value}".

The latter wouldn't work because of the form validation inside "postUsersAction(Request $request)":

// src/Company/MyBundle/Controller/UserController.php
// ...
public function postUsersAction(Request $request)
{   
    $entity = new User();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request); 

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);          
        $em->flush();
        $view = new View($entity);      
    } else {
        $view = View::create($form);    
    }
    return $this->get('fos_rest.view_handler')->handle($view);
}

Thanks for your help!!

1

There are 1 best solutions below

5
On BEST ANSWER

Solution found:

Empty the value returned by "UserType::getName()" will remove the prefixes of parameters. Hence "company_mybundle_user[lastName]" becomes lastName

Before:

// src/Company/MyBundle/Form/UserType.php
// ...
public function getName()
{
    return 'company_mybundle_user';
}

After:

// src/Company/MyBundle/Form/UserType.php
// ...
public function getName()
{
    return '';
}

PS: make sure to clear cache before testing:

php app/console cache:clear