我想呈现一个具有多个相同类实体的表单.我将显示 2 个字段,Price(type=text) 和 Enabled(type=checkbox).
我不知道这些实体有多少,因此表单必须动态获取它们.
我已尝试执行以下操作
public function buildForm(FormBuilderInterface $builder, array $options){$builder->add('价格', '文本', 数组('标签' =>'价格','必需' =>真的))->add('启用','复选框',数组('标签' =>'使用这种货币',));}公共函数 setDefaultOptions(OptionsResolverInterface $resolver){$resolver->setDefaults(array('data_class' =>'奥西里斯实体定价','csrf_protection' =>错误的));}公共函数 getName(){返回定价类型";}
在我的控制器中,我创建了这样的表单:
$pricingForm = $this->createFormBuilder($prices)->add('items','collection',array('必需' =>错误的,'原型' =>真的,'类型' =>新定价类型(),))->getForm();
在我的树枝上:
{% for price in form_pricing %}<h2>价格</h2><div class="row">{{ form_widget(price) }}
{% 结束为 %}
然而,它只带有 h2 价格和带有 class=row 的空 div.我觉得我已经成功了一半,但我不知道如何继续前进.如果有人也知道如何在提交时获取字段,我将不胜感激.
我找到了解决方案,
我在 Controller 中创建表单的方式是错误的!我必须执行以下操作:
$pricingForm = $this->createFormBuilder(array('prices'=>$prices))->add('价格','集合',数组('必需' =>真的,'allow_add' =>真的,'类型' =>新定价类型(),))->getForm();
"allow_add => true" 在使用集合时是必需的,否则它会不将任何 PricingType 实体集合添加到表单中.
然后,因为表单是在控制器内部构建的 "$this->createFormBuilder(array('prices'=>$prices))
" , $prices
数组必须作为一个数组传递,其数组键名与 "->add('prices','collection',array(...)
" 中使用的相同,即 'prices'
这样 Symfony 就会知道在哪里绑定什么.$prices
是一个 Pricing 对象数组 array(0 => new Pricing())
.
在我的 PricingType 中有:
class PricingType 扩展 AbstractType {公共函数 buildForm(FormBuilderInterface $builder, array $options){$builder->add('价格', '文本', 数组('标签' =>错误的,'必需' =>真的))->add('启用','复选框',数组('标签' =>'使用这种货币',));}公共函数 setDefaultOptions(OptionsResolverInterface $resolver){$resolver->setDefaults(array('data_class' =>'XXXXXX实体定价','csrf_protection' =>错误的));}公共函数 getName(){返回定价类型";}}
这里我需要控制标签属性.我找不到方法(如果有人知道,请发布方法).我按如下方式覆盖我的树枝模板:
在顶部我们需要下一行代码:
{% form_theme form_pricing _self %}
然后按如下方式覆盖行和小部件(调试是一场噩梦):
{% block _form_prices_entry_row %}{% 无空间 %}{{ form_widget(form) }}{% endspaceless %}{% 结束块 %}{% 块 _form_prices_entry_widget %}{% 无空间 %}{{ form_row(form.price, { 'label' : form.vars.value.getCurrency().getTitle() } ) }}{{ form_row(form.enabled) }}{% endspaceless %}{% 结束块 %}
然后在正文中,按如下方式渲染表单元素:
{% for price in form_pricing.prices %}<div class="price-row">{{ form_row(price) }}</div>{% 结束为 %}
我真的希望这对你们有帮助.调试尤其是 twig 文件真是一场噩梦,多亏了我聪明的同事,我才做到了.
I want to render a form which has multiple Entities of same Class. I will display 2 fields, Price(type=text) and Enabled(type=checkbox).
I don't know how many I will have of those entities, so form will have to get them dynamically.
I have tried to do the following
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price', 'text', array(
'label' => 'Price',
'required' => true
))
->add('enabled','checkbox',array(
'label' => 'Use this currency',
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'OsirisEntityPricing',
'csrf_protection' => false
));
}
public function getName()
{
return 'pricingtype';
}
And in my Controller I have created my form like this:
$pricingForm = $this->createFormBuilder($prices)
->add('items','collection',array(
'required' => false,
'prototype' => true,
'type' => new PricingType(),
))
->getForm()
;
In my twig I do:
{% for price in form_pricing %}
<h2>Price</h2>
<div class="row">{{ form_widget(price) }}</div>
{% endfor %}
However it comes only with h2 Prices and empty div with class=row. I feel like I am half way there, but I've no idea how to move on. If someone knows how to get fields on submit as well, I will really appreciate it.
I found the solution,
the way I was creating the form in Controller was wrong! I had to do the following:
$pricingForm = $this->createFormBuilder(array('prices'=>$prices))
->add('prices','collection',array(
'required' => true,
'allow_add' => true,
'type' => new PricingType(),
))
->getForm()
;
"allow_add => true" is necessary when working with collection, otherwise it will NOT add any of PricingType collection of entities to the form.
Then, because the form is built inside the controller "$this->createFormBuilder(array('prices'=>$prices))
" , $prices
array must be passed as an array with array keyname same as the one used in "->add('prices','collection',array(...)
" , which is 'prices'
so Symfony will know what to bind where. $prices
is an array of Pricing objects array(0 => new Pricing())
.
In my PricingType I have:
class PricingType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price', 'text', array(
'label' => false,
'required' => true
))
->add('enabled','checkbox',array(
'label' => 'Use this currency',
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'XXXXXXEntityPricing',
'csrf_protection' => false
));
}
public function getName()
{
return 'pricingtype';
}
}
Here I need to have control over label attribute. I could not find the way for it( if anyone knows please post how to). I override my twig template as follows:
On the top we need next line of code:
{% form_theme form_pricing _self %}
Then override row and widget as follows (it was a nightmare to debug):
{% block _form_prices_entry_row %}
{% spaceless %}
{{ form_widget(form) }}
{% endspaceless %}
{% endblock %}
{% block _form_prices_entry_widget %}
{% spaceless %}
{{ form_row(form.price, { 'label' : form.vars.value.getCurrency().getTitle() } ) }}
{{ form_row(form.enabled) }}
{% endspaceless %}
{% endblock %}
In the body then, render form elements as follows:
{% for price in form_pricing.prices %}
<div class="price-row">{{ form_row(price) }}</div>
{% endfor %}
I really hope this will help you guyz. It was a real nightmare to debug especially the twig file, I did it thanks to my clever colleague.
这篇关于Symfony2 同一类中的多个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!