Skip to content

Instantly share code, notes, and snippets.

@daslicht
Forked from WishCow/gist:5318376
Created April 5, 2013 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daslicht/5318387 to your computer and use it in GitHub Desktop.
Save daslicht/5318387 to your computer and use it in GitHub Desktop.
<?php
namespace ME\MainPageBundle\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Form\Form;
class FormErrorResponse extends JsonResponse {
public function __construct(Form $form, $status = 400, $headers = array()) {
$errors = $this->getFormErrors($form);
parent::__construct(array('errors' => $errors), $status, $headers);
}
protected function getFormErrors(Form $form) {
$errors = array();
if ($form->hasChildren()) {
foreach ($form->getChildren() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getFormErrors($child);
}
}
} else {
foreach ($form->getErrors() as $key => $error) {
$errors[] = $error->getMessage();
}
}
return $errors;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment