dongweiben5229 2016-05-29 17:16
浏览 26

在Symfony / FOSRestBundle中更改HTTP方法时的表单中的额外字段

I am building a REST API in Symfony using FOSRestBundle and I've stumbled upon a problem which seems to be trivial, but I can't find a nice solution.

I want to create a controller method to handle PATCH requests:

/**
 * @param Article $article
 * @param Request $request
 *
 * @Patch("/articles/{slug}")
 * @ParamConverter("article", converter="doctrine.orm")
 *
 * @return Response
 */
public function patchAction(Article $article, Request $request)
{
    $form = $this->createForm(new ArticleType(), $article);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $entityManager = $this->get('doctrine.orm.entity_manager');
        $entityManager->merge($article);
        $entityManager->flush();

        $view = $this->view($article)
            ->setTemplate('MyBundle::articleSubmit.html.twig')
            ->setTemplateVar('article')
        ;

        return $this->handleView($view);
    }

    $view = $this->view($form)
        ->setTemplate('MyBundle::articleForm.html.twig')
        ->setTemplateVar('form')
        ->setTemplateData(array('article' => $article))
    ;

    return $this->handleView($view);
}

I've configured form type like this, setting method option to PATCH:

namespace MyBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('content', 'textarea');
    }

    public function getName()
    {
        return '';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyBundle\Entity\Article',
            'method' => 'PATCH',
        ));
    }
}

When I want to see this working, everything goes fine as long as I make a real PATCH request (for example using Open HttpRequester). But as most browsers support only GET and POST method, Symfony makes a trick adding _method parameter to regular POST request, and that's where my problem begins, because when testing this in browser I get the message:

This form should not contain extra fields.

This is obviously because ArticleType::getName() returns empty string, but I would like my request parameters look more like content=foo than like article[content]=foo, as it's supposed in REST API. That's why I want this method to return blank value.

This works normally either when I change the action to POST, or when I set the form's name, or when I make a real PATCH request, but not when I'm sending a POST request with _method parameter.

I've managed to solve this problem creating a body listener that removes fields having names prefixed by _, but that's more a dirty hack than a nice and clean solution.

I'm sure there must be some better workaround, thank you in advance for any help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入