Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. /**
  2. * @Route("/contact", name="_demo_contact")
  3. * @Template()
  4. */
  5. public function contactAction(Request $request)
  6. {
  7. $contact = new Contact();
  8. // Set a default for message inside the data object itself.
  9. // I expect that if message is missing in the request, this value will be used as the default
  10. $contact->setMessage("Default message from controller");
  11. // Create the form with $contact as the initial data. Disable csrf to make the test more straightforward.
  12. $form = $this->createForm(new ContactType(), $contact, ['csrf_protection' => false]);
  13. $form->handleRequest($request);
  14.  
  15. if ($form->isValid()) {
  16. return new Response("Good!", 200);
  17. } else if ($form->isSubmitted()) {
  18. // $message always returns a validation error even though I've set it on the underlying data object
  19. return new Response((string)$form->getErrors(true), 400);
  20. }
  21.  
  22. return array('form' => $form->createView());
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement