【问题标题】:REST json api with symfony 2.7, form not validating even when filled带有 symfony 2.7 的 REST json api,即使填写也不会验证表单
【发布时间】:2017-12-11 17:56:28
【问题描述】:

我试图弄清楚为什么当我创建一个带有来自 json 请求的值的预填充实体的表单时表单没有验证。

这是 Symfony 中已经配置了 FosRestBundle 的控制器:

public function createAction(Request $request)
{
    $house = new House();
    $house->setTitle($request->get('title'));
    $house->setDescription($request->get('description'));
    $house->setPostcode($request->get('postCode'));
    $house->setPhoneNumber((int) $request->get('phoneNumber'));
    $availability = $request->get('available') ? true : false;
    $house->setAvailability($availability);

    $form = $this->createCreateForm($house);

    $form->handleRequest($request);

    $response = new JsonResponse();

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($house);
        $em->flush();

        return $response->setData(array(
            'success' => true
        ));
    }

    return $response->setData(array(
        'success' => false,
        'errors' => $this->getFormErrors($form)
    ));
}

private function createCreateForm(House $entity)
{
    $form = $this->createForm(new HouseType(), $entity, array(
        'action' => $this->generateUrl('houses_create'),
        'method' => 'POST',
        'csrf_protection' => false
    ));

    return $form;
}

yaml 配置文件:

# app/config/config.yml
fos_rest:
    param_fetcher_listener: true
    body_listener: true
    routing_loader:
        default_format: json
    exception:
        enabled: true
    # configure the view handler
    view:
        force_redirects:
            html: true
        formats:
            json: true
            xml: true
        templating_formats:
            html: true
    # add a content negotiation rule, enabling support for json/xml for the entire website
    format_listener:
        enabled: true
        rules:
            - { path: ^/, priorities: [ json, xml, html ], fallback_format: html, prefer_extension: false }

例如,如果我执行$form->get('title')->getData(),我可以看到表单已正确填写但仍未通过验证,当我执行$this->getFormErrors($form) 时,我只会得到一个空数组。

知道如何调试这个问题吗?

【问题讨论】:

    标签: php json symfony fosrestbundle


    【解决方案1】:

    为了接收json,您需要启用body listener功能。

    在你的config.yml

    fos_rest:
        body_listener: true
    

    你也应该check the documentation for advanced usage

    【讨论】:

    • 感谢您的回答,我已经更新了 yml 配置文件,但仍然遇到同样的问题。
    • 您发送的请求是否包含标头“Content-Type: application/json”?
    • 正如 sw0rdfishhhhh 所说,我刚刚根据最新的调试编辑了问题,谢谢
    【解决方案2】:

    当您启动 $form 时,我注意到一个错字:“createCreateForm” -> 它应该只是“createForm”。

    如果这不起作用,请检查 "if ($form->isValid()) { " 部分之后来自 $form->getData() 的值。我怀疑框架不知道如何处理请求(不是来自标准表单)。在这种情况下,您可以手动设置实体上的值并保存它。

    $house->setStuff($request->request->get('stuff');
    ...
    $em->persist($house);`$em->flush();`
    

    【讨论】:

    • $form->getData()的内容怎么样?只需 var_dump 它。如果它为空或没有帮助,请尝试使用调试器逐步进行(如果您设置了 xdebug)。您需要的值很可能在 $request 变量中。
    • 今天刚刚调试了这个。看起来值在表单中,但 $form->isValid() 仍然返回 false,当我执行 $this->getFormErrors($form) 时,我得到一个空数组。
    • 刚刚编辑了相应问题的标题和描述。到目前为止,谢谢。
    • 看看这个:stackoverflow.com/questions/11208992/…。在调用 isValid() 之前仔细检查您对实体的约束和在实体上设置的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多