Created
February 21, 2015 10:43
-
-
Save lunetics/9e6865f6596717685731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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