gpt4 book ai didi

php - Symfony2 表单用数据预填充字段

转载 作者:可可西里 更新时间:2023-11-01 13:50:21 25 4
gpt4 key购买 nike

暂时假设此表单使用 ZooCollection 中的一个虚构的 Animal 文档对象类,它在 中只有两个属性(“名称”和“颜色”) .

我正在寻找一个工作简单愚蠢的解决方案,以预填充表单字段给定的对象auto -神奇地(例如更新?)。

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 生成的代码和学说控制台命令 (doctrine:generate:crud) 中学习总是一个好主意。您可以了解处理此类请求的想法和方式。

关于php - Symfony2 表单用数据预填充字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20500115/

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