diff --git a/Controller/RestFormController.php b/Controller/RestFormController.php index 7149bff..fca832b 100644 --- a/Controller/RestFormController.php +++ b/Controller/RestFormController.php @@ -17,6 +17,10 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormTypeInterface; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; + +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\RedirectResponse; /** * Form Controller helps implementing Restful Controllers @@ -48,7 +52,7 @@ abstract class RestFormController extends Controller protected $form; /** - * @return SimpleThings\FormSerializerBundle\Serializer\FormSerializer + * @return \SimpleThings\FormSerializerBundle\Serializer\FormSerializer */ protected function getFormSerializer() { diff --git a/Controller/RestFormHelper.php b/Controller/RestFormHelper.php index 06a5fdb..5e63203 100644 --- a/Controller/RestFormHelper.php +++ b/Controller/RestFormHelper.php @@ -17,6 +17,10 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormTypeInterface; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; + +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\RedirectResponse; /** * Form trait helper implementing Restful Controllers @@ -84,7 +88,7 @@ protected function renderForm(FormInterface $form = null, array $variables = arr $form = $form ?: $this->form; $format = $this->getRequest()->getRequestFormat(); - if ($format === "html") { + if ($format === 'html') { $variables['form'] = $form->createView(); $variables['data'] = $form->getData(); @@ -119,7 +123,7 @@ protected function renderForm(FormInterface $form = null, array $variables = arr */ protected function flash() { - if ($this->getRequest()->getRequestFormat() !== "html") { + if ($this->getRequest()->getRequestFormat() !== 'html') { return new FlashBag; // dummy flush-bag, to keep the fluent } @@ -150,8 +154,8 @@ protected function redirectRoute($routeName, $parameters = array(), $statusCode $link = $this->generateUrl($routeName, $parameters, $absolute); if ($statusCode === 201 || $statusCode === 204) { - if ($this->getRequest()->getRequestFormat() !== "html") { - return new Response("", $statusCode, array("Location" => $link)); + if ($this->getRequest()->getRequestFormat() !== 'html') { + return new Response('', $statusCode, array('Location' => $link)); } $statusCode = 301; } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index d948027..93e3191 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -30,6 +30,7 @@ + diff --git a/Serializer/FormSerializer.php b/Serializer/FormSerializer.php index f2f7d43..5aa4a73 100644 --- a/Serializer/FormSerializer.php +++ b/Serializer/FormSerializer.php @@ -17,18 +17,23 @@ use Symfony\Component\Form\FormTypeInterface; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormError; use Symfony\Component\Serializer\Encoder\EncoderInterface; use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Translation\TranslatorInterface; + class FormSerializer implements FormSerializerInterface { private $factory; private $encoder; private $options; + private $translator; - public function __construct(FormFactoryInterface $factory, EncoderInterface $encoder, SerializerOptions $options = null) + public function __construct(TranslatorInterface $translator, FormFactoryInterface $factory, EncoderInterface $encoder, SerializerOptions $options = null) { + $this->translator = $translator; $this->factory = $factory; $this->encoder = $encoder; $this->options = $options ?: new SerializerOptions; @@ -112,12 +117,17 @@ public function serialize($object, $typeBuilder, $format) return $this->encoder->encode($data, $format); } + private function getErrorMessage(FormError $error) + { + return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); + } + private function serializeFormError(FormInterface $form) { $result = array(); foreach ($form->getErrors() as $error) { - $result['error'][] = $error->getMessage(); + $result['errors'][] = $this->getErrorMessage($error); } foreach ($form->getChildren() as $child) {