gpt4 book ai didi

php - 将 FOSRestBundle 与表单一起使用

转载 作者:可可西里 更新时间:2023-11-01 12:38:55 24 4
gpt4 key购买 nike

菜鸟,关于 FOSRestBundle、JMSSerializerBundle 和模板的问题。

我正在尝试替换一些当前通过 twig 输出 json 的现有代码以使用 FOSRestBundle。这在从 Controller 传递的内容最初是数组中的情况下取得了成功,但现在我试图将表单传递给 FOSRestBundle,结果是我的值永远不会返回。

下面的代码复制了这个场景

/**
* my sample get action
* @View(templateVar="form")
*/
public function getAction($id)
{
...
$form = $this->createFormBuilder(array('myValue' => 'SOMEVALUE'))
->add('myValue', 'hidden')
->getForm();

$view = FOSView::create($form);
$view->setFormat('json');

return $this->get('fos_rest.view_handler')->handle($view);
}

返回

{"children":{"_token":[],"myValue":[]}}

我希望在这里看到的是这样的:

{"children":{"_token": "mylongtoken","myValue": "SOMEVALUE"}}

我的代码一直基于 LiipHelloBundle 中的示例,除非我弄错了这与他们提供的示例相匹配?我哪里出错了?

最佳答案

好吧,这并不是我期望的 Symfony 中最干净的解决方案,但它似乎是这样工作的:

$form->createView()->get('form')->get('form')->getChild('myValue')->get('choices')

返回 myValue 实体:

{"28":"Default1","103":"test"}

关于php - 将 FOSRestBundle 与表单一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10591596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com