Skip to content

Instantly share code, notes, and snippets.

@lunetics
Created February 21, 2015 10:43
Show Gist options
  • Save lunetics/9e6865f6596717685731 to your computer and use it in GitHub Desktop.
Save lunetics/9e6865f6596717685731 to your computer and use it in GitHub Desktop.
<?php
namespace Lunetics\AppBundle\Form\Response;
class FormInterceptor {
public function getFormErrorsToJson(Form $form)
{
$errors = array();
$translator = $this->get('translator');
if (count($form->getErrors())) {
foreach ($form->getErrors() as $error) {
$errors[$form->getName()]['errors'][] = $translator->trans(
$error->getMessageTemplate(),
$error->getMessageParameters(),
$this->getValidatorTranslationDomain()
);
}
}
if ($form->count()) {
foreach ($form->all() as $child) {
if ($child->count()) {
if ($childErrors = $this->getFormErrorsForJson($child)) {
$errors[$form->getName()]['childErrors'] = array_merge_recursive(
isset($errors[$form->getName()]['childErrors'])
? $errors[$form->getName()]['childErrors']
: array(),
$childErrors
);
}
} else if (count($child->getErrors())) {
$translationDomain = $this->getValidatorTranslationDomain();
$errors[$form->getName()]['childErrors'][$child->getName()] = array_map(
function($error) use ($translator, $translationDomain) {
return $translator->trans(
$error->getMessageTemplate(),
$error->getMessageParameters(),
$translationDomain
);
}, $child->getErrors()
);
}
}
}
return $errors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment