donglun4521 2014-08-27 15:39
浏览 52
已采纳

嵌入表单和选择列表

Site form

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 ?

  • 写回答

1条回答 默认 最新

  • dongxian0320 2014-08-27 16:07
    关注

    I think you can resolve this like this:

    <kbd>SiteType</kbd>

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题