mardi 31 mars 2015

Symfony2 multiple Entities of same class in one Form

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 they 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' => 'Osiris\Entity\Pricing',
'csrf_protection' => false
));
}

public function getName()
{
return 'pricingtype';
}
}


And in my Controller I create my form as follows:



$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.


Aucun commentaire:

Enregistrer un commentaire