简体   繁体   中英

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.

您没有通过标记启用表单错误处理程序,而是通过将jms_serializer.form_error_handler.class参数设置为指向您的类来jms_serializer.form_error_handler.class

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM