Skip to content

Instantly share code, notes, and snippets.

@Taluu
Created October 21, 2013 13:41
Show Gist options
  • Select an option

  • Save Taluu/7084065 to your computer and use it in GitHub Desktop.

Select an option

Save Taluu/7084065 to your computer and use it in GitHub Desktop.
<?php
namespace MyBundle\Exception;
use \InvalidArgumentException;
use Symfony\Component\Form\FormError,
Symfony\Component\Form\FormInterface;
/**
* Thrown when a form is not valid.
*
* This exception should be used with an ApiException, but as a composition.
*
* @author Baptiste Clavié <[email protected]>
*/
class FormNotValidException extends InvalidArgumentException
{
/** @var FormInterface */
private $form;
public function __construct(FormInterface $form)
{
$this->form = $form;
}
public function getErrors()
{
return $this->getErrorsAsArray($this->form);
}
private function getErrorsAsArray(FormInterface $form)
{
$errors = ['errors' => array_map(function (FormError $error) { return $error->getMessage(); }, $form->getErrors()),
'children' => []];
foreach ($form as $key => $child) {
if ($childErrors = $this->getErrorsAsArray($child)) {
$errors['children'][$child->getName()] = $this->getErrorsAsArray($child);
}
}
if (0 === count($errors['errors'])) {
unset($errors['errors']);
}
if (0 === count($errors['children'])) {
unset($errors['children']);
}
if (!isset($errors['errors']) && isset($errors['children'])) {
return $errors['children'];
}
if (isset($errors['errors']) && !isset($errors['children'])) {
return $errors['errors'];
}
return $errors;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment