Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSRestBundle and JMSSerializer custom form error handler

I have written a custom Form Handler for JMSSerializerBundle that I'm using with FOSRestBundle. According to the documentation it should be as easy as tagging a service correctly. But my custom handler never gets used.

Here's the handler:

<?php

namespace AppBundle\Handler;

use JMS\Serializer\Handler\FormErrorHandler as JMSFormErrorHandler;

class FormErrorHandler extends JMSFormErrorHandler
{
    public function serializeFormToJson(\JMS\Serializer\JsonSerializationVisitor $visitor, \Symfony\Component\Form\Form $form, array $type)
    {
        $this->convertFormToArray($visitor, $form);
    }

    private function getErrorMessage(FormError $error)
    {
        if (null !== $error->getMessagePluralization()) {
            return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), 'validators');
        }

        return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators');
    }

    private function convertFormToArray(GenericSerializationVisitor $visitor, Form $data)
    {
        $isRoot = null === $visitor->getRoot();

        $form = $errors = array();
        foreach ($data->getErrors() as $error) {
            $errors[] = $this->getErrorMessage($error);
        }

        if ($errors) {
            $form['errors'] = $errors;
        }

        $children = array();
        foreach ($data->all() as $child) {
            if ($child instanceof Form) {
                $children[$child->getName()] = $this->convertFormToArray($visitor, $child);
            }
        }

        if ($children) {
            $form = array_merge($form , $children);
        }

        if ($isRoot) {
            $visitor->setRoot($form);
        }

        return $form;
    }
}

Here's the service registration

services:     
    my_form_error_handler:
        class: AppBundle\Handler\FormErrorHandler
        arguments: ["@translator"]
        tags:
            - {name: jms_serializer.subscribing_handler}

I don't need to change much so for the most part I just extend the original and changed the functions I needed to change.

There are no errors. Everything executes as if no overriding class existed and it just uses the default FormErrorHandler found in the JMSSerializer. Does this have anything to do with also using the FOSRestBundle? For giggles, in a random controller I tried $this->get('my_form_error_handler') and that worked, so I know the service is registered. Any help appreciated.

Thanks.

like image 816
Squeegy Avatar asked Sep 16 '26 22:09

Squeegy


1 Answers

you do not enable the form error handler by tagging, but by setting the jms_serializer.form_error_handler.class parameter to point to your class:

parameters: jms_serializer.form_error_handler.class: AppBundle\Handler\FormErrorHandler

like image 146
Tarjei Huse Avatar answered Sep 18 '26 11:09

Tarjei Huse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!