暂时假设该形式使用了一个虚构的Animal
文档对象类来自ZooCollection
只有两个属性(“名称”和“颜色”)symfony2 /questions/tagged/symfony2.
我正在寻找一个工作简单愚蠢的解决方案, to pre-fill the 表单字段与给定的对象自动地神奇地(例如,更新?)。
Acme/DemoBundle/Controller/CustomController
:
public function updateAnimalAction(Request $request)
{
...
// Create the form and handle the request
$form = $this->createForm(AnimalType(), $animal);
// Set the data again << doesn't work ?
$form->setData($form->getData());
$form->handleRequest($request);
...
}
您应该加载要更新的动物对象。 createForm() 将使用加载的对象来填充表单中的字段。
假设您使用注释来定义路线:
/**
* @Route("/animal/{animal}")
* @Method("PUT")
*/
public function updateAnimalAction(Request $request, Animal $animal) {
$form = $this->createForm(AnimalType(), $animal, array(
'method' => 'PUT', // You have to specify the method, if you are using PUT
// method otherwise handleRequest() can't
// process your request.
));
$form->handleRequest($request);
if ($form->isValid()) {
...
}
...
}
我认为从 Symfony 和学说控制台命令生成的代码中学习总是一个好主意(学说:生成:crud http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_crud.html)。您可以了解处理此类请求的想法和方式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)