Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Xxx\Component\HttpFoundation;
  4.  
  5. use Symfony\Component\Form\Form;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7.  
  8. class JsonFormResponse extends JsonResponse
  9. {
  10. public function __construct(Form $data, $status = 422, $headers = array())
  11. {
  12. $errors = [$data->getName() => []];
  13.  
  14. if (count($formErrors = $this->getFormErrors($data))) {
  15. $errors[$data->getName()] = [
  16. 'errors' => $formErrors
  17. ];
  18. }
  19.  
  20. foreach ($data as $field) {
  21. if (count($fieldErrors = $this->getFormErrors($field))) {
  22. $errors[$data->getName()][$field->getName()] = [
  23. 'errors' => $fieldErrors
  24. ];
  25. }
  26. }
  27.  
  28. parent::__construct($errors, $status, $headers);
  29. }
  30.  
  31. private function getFormErrors($form) {
  32. $errors = [];
  33. foreach ($form->getErrors() as $error) {
  34. $errors[] = $error->getMessageTemplate();
  35. }
  36.  
  37. return $errors;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement