有没有办法检查 Controller 中是否存在表单字段?
我有几个提交按钮,但根据对象中的数据,将显示和创建关联的按钮。
表单类型.php
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
/** @var ObjectInfo $tab */
$tab = $event->getData();
$form = $event->getForm();
if (some condition) {
//No Value has been set or NULL
$form->add('submit_second', 'submit', array(
'label' => 'submit',
))
}
Controller .php
if ($overviewForm->get('submit_second')->isClicked()) {
// do something
}
我也试过
if (
!is_null($overviewForm->get('submit_second')) &&
$overviewForm->get('submit_second')->isClicked()
) {
提交时我得到
错误
Child "submit_second" does not exist.
最佳答案
如 $form->has('field')
(由 @jahller
建议)不起作用,您可以尝试将“ 某些条件 ”的逻辑放入您的实体/模型的方法中,然后在您需要的地方调用该方法。
您应该可以在 FormType
中访问您的模型/实体和 Controller
.
关于forms - Symfony 检查表单字段存在于 Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32841795/