mercredi 17 septembre 2014

Remove elements from request before processing the form


Vote count:

0




I'm creating a simple CRUD for managing users of one application. Users are created and managed by FOSUserBundle. In the edition I do not allow any to edit/change the username and the email so I do not show these fields in the form being edited. This is the code with which I'm handling the edition :



// Create the form and set the right data
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('UserBundle:User')->find($id);

$form = $this->createForm(new UserType(), $user, array(
'action' => $this->generateUrl('update-user', array('id' => $id)),
'method' => 'POST',
));

return array(
'form' => $form->createView(),
'user' => $user
);
}

// Perform the update action and do the rest of the logic
public function updateAction(Request $request, $id = NULL)
{
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository("UserBundle:User")->find($id);

$form = $this->createForm(new UserType(), $user);
$form->handleRequest($request);

$response = array();
$response['status'] = TRUE;

if ($form->isValid())
{
$userManager = $this->container->get('fos_user.user_manager');
$userManager->updateUser($user);
$response['message'] = $this->get('translator')->trans('update.success', array('%element%' => 'el usuario'));
}
else
{
$response['message'] = $this->get('translator')->trans('update.fail', array('%element%' => 'el usuario'));
$response['error'] = $this->getFormErrors($form);
$this->get('ladybug')->log($response['error']);
$response['status'] = FALSE;
}

return new JsonResponse($response);
}


But since username and email fields are not present in somehow them are getting to the update action, then these fields are updated with blank values​​, the only thing I can think to solve this problem is to delete this parameters from the request before making the handleRequest but I have no idea how, any help?


PS: Any ideas better than mine or any suggestion on the code is welcome!!



asked 1 min ago

ReynierPM

1,043






Remove elements from request before processing the form

Aucun commentaire:

Enregistrer un commentaire