动态删除数据字段

Remove data fields dynamically

我正在尝试动态删除字段

$form->remove('email');

由于它有效,我在 twig 文件中收到一条错误消息

An exception has been thrown during the rendering of a template ("Argument 1 passed to Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an instance of Symfony\Component\Form\FormView, null given, called in /Users/xom/Projects/tt/var/cache/twig/9/5/95df63c0279f2b5a5adc85a478d9689d895a956a73987966af771d1f551ece8b.php on line 137 and defined") in "Back/Products/add.html.twig" at line 34.

这是 twig 文件第 34 行的代码

 {{ form_row(form.email, { 'attr': {'class': 'form-control'} }) }}

我知道出现此错误的原因是因为调用 $form->remove('email'])

而未设置 form.email

只需检查电子邮件字段是否可用:

{% if form.email is defined %}
    {{ form_row(form.email, { 'attr': {'class': 'form-control'} }) }}
{% endif %}