0

我想以一种特殊的方式显示表单选择(单选按钮)。

简短的解释:

我需要在选择小部件中显示实体的许多属性,而不仅仅是名称(_toString)和值(id)。

扩展解释:

我不会浪费时间解释我的实体,因为它们工作正常,我对它们没有任何问题。

我有一个SalonWeb Entity,它与Album Entity有OneToOne 关系。此外,相册实体Foto 实体具有OneToMany 关系,并包含一个 $fotos ArrayCollection 和一个链接 $foto_id 的 $foto_principal 属性。

因此,通过正确的学说查询,我可以访问如下内容:

$salon_web->getAlbum()->getFotoPrincipal();

或者,在 TWIG 中:

salonWeb.album.fotoPrincipal

到现在为止都是正确的。

我想将这张照片(英文照片)显示为表单选择标签,所以我做了这个代码(正在工作)

在表单生成器中:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('salones', 'entity', array(
            'class' => 'CommonBundle:SalonWeb',
            'required' => true,
            'expanded' => true,
            'query_builder' => function(EntityRepository $er)
                {
                    return $er->getQueryBuilderVisiblesContacto();
                },
            'property' => 'album.foto_principal'
        ))
    // More code...
}

...在 TWIG 模板中:

  <div>
      {{ form_errors(formulario.salones) }}
      {{ form_label(formulario.salones) }}
      {% for childSalon in formulario.salones %}
      <label><img src="/uploads/galeria/{{ childSalon.vars.label }}" alt="" />{{ form_widget(childSalon) }}</label>

      {% endfor %}
  </div>
  {{ form_widget(formulario) }}

直到这里一切正常。但问题是我只能在表单选择中显示一个属性(在本例中是 SalonWeb 实体的 album.foto_principal 属性)

我想展示这样的东西:

  <div>
      {{ form_errors(formulario.salones) }}
      {{ form_label(formulario.salones) }}
      {% for childSalon in formulario.salones %}
        <label><img src="/uploads/galeria/{{ childSalon.whatever.name }}" alt="" />{{ childSalon.whatever.address ~ ' ' ~ childSalon.whatever.anotherSalonWebProperty }}
        <div>{{ childSalon.whatever.theLastProperty }}</div>
        {{ form_widget(childSalon) }}</label>

      {% endfor %}
  </div>
  {{ form_widget(formulario) }}
4

1 回答 1

2

最后,受这篇文章的解决方案的启发,我找到了一种方法:

Symfony 2 创建具有 2 个属性的实体表单字段

向我的 SalonWeb 实体添加一个方法:

    //...
    public function getFormChoiceImageAndLabelProperties()
    {
        return array(
            'image_src' => $this->getAlbum()->getFotoPrincipal(),
            'label' => $this->getDireccionParcial(),
            'another_property' => $this->getWhatever(),
        );
    }

将我的表单构建器选择属性从

'property' => 'album.foto_principal'

'property' => 'form_choice_image_and_label_properties'

...在 TWIG 模板中:

  <div>
      {{ form_errors(formulario.salones) }}
      {{ form_label(formulario.salones) }}
      {% for childSalon in formulario.salones %}
      <label>
        <img src="/uploads/galeria/{{ childSalon.vars.label['image_src'] }}" alt="{{ childSalon.vars.label['label'] }}" />
        <div>
          <span>{{ childSalon.vars.label['label'] }}</span>
        </div>
        <div>
          <span>{{ childSalon.vars.label['another_property'] }}</span>
        </div>
        {{ form_widget(childSalon) }}
      </label>

      {% endfor %}
  </div>
  {{ form_widget(formulario) }}
  {{ form_widget(formulario) }}
于 2013-07-03T08:59:42.293 回答