[英]Symfony2 twig throws render error on conditionally added form fields
I currently getting an error if I try to render a conditionally added form element in twig. 如果尝试在树枝中渲染有条件添加的表单元素,则当前出现错误。 The form element was added (or not) through the form event listener mechanism and should only add the form element if a specific form option is set.
表单元素是通过表单事件侦听器机制添加(或不添加)的,并且仅在设置了特定表单选项的情况下才添加表单元素。
Argument 1 passed to Symfony\\Component\\Form\\FormRenderer::searchAndRenderBlock() must be an instance of Symfony\\Component\\Form\\FormView, null given
传递给Symfony \\ Component \\ Form \\ FormRenderer :: searchAndRenderBlock()的参数1必须是Symfony \\ Component \\ Form \\ FormView的实例,给定null
<?php
namespace Vendor\ProjectBundle\Form\Type;
// [...]
abstract class AbstractContextualInfoFormType extends AbstractFormType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('user', new UserFormType($this->getTranslator(), $this->getDoctrine()), array('error_bubbling' => true, 'validation_groups' => 'ValidationGroup'));
$creditcardForm = new CreditcardFormType($this->getTranslator(), $this->getDoctrine());
$creditcardForm->setProcess($options['process']);
$creditcardForm->setProvider($options['provider']);
if (array_key_exists('cvc', $options)) {
$creditcardForm->setRequireCvc($options['cvc']);
}
if (array_key_exists('types', $options)) {
$creditcardForm->setAllowedCreditcardTypes($options['types']);
}
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($options) {
if (!array_key_exists('disable_creditcard', $options) OR (array_key_exists('disable_creditcard', $options) AND $options['disable_creditcard'] === true)) {
$creditcardForm = new CreditcardFormType($this->getTranslator(), $this->getDoctrine());
$creditcardForm->setProcess($options['process']);
$creditcardForm->setProvider($options['provider']);
if (array_key_exists('cvc', $options)) {
$creditcardForm->setRequireCvc($options['cvc']);
}
if (array_key_exists('types', $options)) {
$creditcardForm->setAllowedCreditcardTypes($options['types']);
}
$form = $event->getForm();
$form->add('creditcard', $creditcardForm, array('error_bubbling' => true));
}
}
);
}
}
// [...]
As you can see i try to add the credit card form only if the option disable_creditcard
is not set. 如您所见,只有在未设置
disable_creditcard
选项的情况下,我才尝试添加信用卡表格。 This all works fine until the moment I try to browse the page where I implemented the form: 在尝试浏览实现表单的页面之前,所有这些都可以正常工作:
{% if not disable_creditcard %}
<div id="detail_creditcard" class="creditcard">
<legend>{{ 'creditcard.content.title'|trans }}</legend>
<div class="alert alert-info">
<i class="icon-info-sign"></i>
Bla bla bla text
</div>
**{{ form_row(form_data.creditcard.owner) }}**
{{ form_row(form_data.creditcard.number) }}
{{ form_row(form_data.creditcard.type) }}
{{ form_row(form_data.creditcard.validity) }}
{{ form_rest(form_data.creditcard) }}
</div>
{% endif %}
I also tried it with a surrounded conditional-if, but that doesn't work at all... I think twig needs the "not defined" creditcard form element here but cannot find it. 我也用一个包围的有条件的if进行了尝试,但这根本不起作用...我认为twig在这里需要“未定义”的信用卡形式元素,但找不到它。
What is the right way for doing this? 什么是正确的方法? I would appreciate any help from you.
谢谢您的帮助。 :-)
:-)
Thanks! 谢谢!
try this: 尝试这个:
{% if form_data.creditcard is defined %}
... your conditional code here
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.