Symfony 实体类型 - 如何获得选择的值

Symfony entity type - how to get the chosen value

...        $form = $this->createFormBuilder($foo)
        ->add('deviceId', EntityType::class, array(
            'class' => 'FooBundle:Foo',
            'choice_label' => 'deviceId'
        ))
...

如何找出用户为 deviceId 选择了哪个选项。

...
if ($form->isSubmitted() && $form->isValid()) {
/*Getting the value that was selected
and doing something with it
for exampe removing it from the database*/
}
...

我认为在验证表单是否已提交以及它是否有效之前,您已经这样做了:

$form->handleRequest($request);

在这种情况下,您可以直接访问它,因为您的对象已更新

$yourObject->getDeviceId()

您还可以像这样访问表单数据:

$form->getData();