Skip to content

Instantly share code, notes, and snippets.

@jmcclell
Last active August 29, 2015 14:14
Show Gist options
  • Save jmcclell/3d013ed1cbd0ca593d76 to your computer and use it in GitHub Desktop.
Save jmcclell/3d013ed1cbd0ca593d76 to your computer and use it in GitHub Desktop.
/**
* @Route("/contact", name="_demo_contact")
* @Template()
*/
public function contactAction(Request $request)
{
$contact = new Contact();
// Set a default for message inside the data object itself.
// I expect that if message is missing in the request, this value will be used as the default
$contact->setMessage("Default message from controller");
// Create the form with $contact as the initial data. Disable csrf to make the test more straightforward.
$form = $this->createForm(new ContactType(), $contact, ['csrf_protection' => false]);
$form->handleRequest($request);
if ($form->isValid()) {
return new Response("Good!", 200);
} else if ($form->isSubmitted()) {
// $message always returns a validation error even though I've set it on the underlying data object
return new Response((string)$form->getErrors(true), 400);
}
return array('form' => $form->createView());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment