I have a form, SiteType, with an other form, DomainType, embbed. But when I tried to display in the site form the domain name field, which is a choice list, it appears 3 times (each list contains all the domains in the DB) instead of one time.
This is my SiteType :
class SiteType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'label' => 'Nom du site',
'required' => true
))
->add('nameBundle', 'text', array(
'label' => 'Nom du bundle du site',
'required' => true
))
->add('numClient', 'integer', array(
'label' => 'Numéro client du site',
'required' => true
))
->add('domains', 'collection', array(
'type' => new DomainType(),
));
}
...
}
and my DomainType:
class DomainType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('domainName','entity', array(
'class' => 'EliophotBackBundle:Domain',
'property' => 'domainName',
'label' => 'Nom du domaine'
));
}
...
}
and the form where I display the form :
<form action="{{ path('site_create') }}" method="post">
{{ form_row(form.name) }}
{{ form_row(form.nameBundle) }}
{{ form_row(form.numClient) }}
{% for domain in form.domains %}
{{ form_row(domain.domainName) }}
{% endfor %}
{{ form_rest(form) }}
<div class="btn-group">
<button type="submit" class="btn btn-success">Ajouter</button>
</div>
</form>
My SiteController :
public function newSiteAction()
{
$site = new Site();
$repository = $this->get('doctrine')
->getRepository('TestBackBundle:Domain');
$domains = $repository->findAll();
foreach($domains as $domain) {
$domainObject = new Domain();
$domainObject->setDomainName($domain->getDomainName());
$site->getDomains()->add($domainObject);
}
$newForm = $this->createForm(new SiteType(), $site);
return $this->render('TestBackBundle:Site:new_site.html.twig', array(
'site' => $site,
'form' => $newForm->createView(),
));
}
I would like to diplay only one choice list with all the domains name... How can I do this ?
I think you can resolve this like this:
SiteType
$builder
->add('name', 'text', array(
'label' => 'Nom du site',
'required' => true
))
->add('nameBundle', 'text', array(
'label' => 'Nom du bundle du site',
'required' => true
))
->add('numClient', 'integer', array(
'label' => 'Numéro client du site',
'required' => true
))
->add('domains','entity', array(
'class' => 'EliophotBackBundle:Domain',
'property' => 'domainName',
'label' => 'Nom du domaine',
'multiple' => true
));
In this case you wouldn't have need for DomainType
. As for the controller can you clarify this snippet:
$domains = $repository->findAll();
foreach($domains as $domain) {
$domainObject = new Domain();
$domainObject->setDomainName($domain->getDomainName());
$site->getDomains()->add($domainObject);
}
Why are you fetching and then reconstructing all the domains? Are the domains
from Site
entityt not of type TestBackBundle:Domain
? If they are in fact, you could just:
$domains = $repository->findAll();
$site->setDomains(new ArrayCollection($domains)); // don't forget sto `use` ArrayCollection
Hope this helps a bit...