mat*_*e64 5 php forms controller symfony doctrine-mongodb
假设这个表单利用了一个虚构的Animal文档对象类ZooCollection,它只包含symfony2中的两个属性("name"和"color").
我正在寻找一个有效的简单愚蠢的解决方案,用自动神奇的方式预先填充给定对象的表单字段(例如,用于更新?).
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);
...
}
Run Code Online (Sandbox Code Playgroud)
您应该加载要更新的动物对象.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()) {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
我认为从Symfony和doctrine控制台命令(doctrine:generate:crud)生成的代码中学习它总是一个好主意.您可以了解这个想法以及处理此类请求的方式.