FOSRestBundle and JMSSerializer custom form error handler

旧时模样 提交于 2019-12-01 08:17:48

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/31796469/fosrestbundle-and-jmsserializer-custom-form-error-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!