我的表单中有两个下拉菜单。第二个下拉菜单中的选项取决于第一个下拉菜单中的选定值。如果验证失败,则需要获取第一个下拉菜单的值,以便在第二个下拉列表中设置选项。那么,当验证失败时,如何在表单类型类中接收过帐的值?
答案 0 :(得分:1)
$form = $this->createForm(MyFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* Do your stuff when form is valid
*/
} else {
/**
* Here you can get the input data even failed with $form->getData()
*/
dump($form->getData());
}
$form->getData()
为我提供了在else中没有失败的输入值:
array:4 [▼
"first_dropdown" => "hello"
]
答案 1 :(得分:0)
$ advert =新的Advert();
$form = $this->createForm(AdvertType::class, $advert, ['method' => 'POST'])
->handleRequest($request);
if (false === $form->isSubmitted()) {
$form->submit([]);
}
if (false === $form->isValid()) {
return ['form' => $form];
}
$manager = $this->getDoctrine()->getManager();
$this->appendTags($advert);
$manager->persist($advert);
$manager->flush();
return $advert;
答案 2 :(得分:0)
在我的表单类型类中,我需要下面类似的内容。我在buildForm()方法中使用了DocType:
$request = Request::createFromGlobals();
$myObjectId = $request->request->get('doc')['object']; // selected option of the first dropdown
$object = ($obectId) ? $objectRepository->find($objectId) : null;
然后我可以使用获取的对象,通过查询生成器填充第二个下拉列表。