2014-02-28 71 views
1

我想自定義的應用程序一樣,所有的多重選擇:將類和屬性添加到Symfony2中的所有多個選擇?

<select class="selectpicker show-tick" data-size="auto"> 
... 
</select> 

我應該怎麼辦呢?

編輯ncrocfer

這是我的構建形式方法: 這是不完整的,缺少一些東西...

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('title') 
     ->add('misc') 
     ->add('url') 
     ->add('attachment', 'file') 
     ->add('time_estimated') 
     ->add('started_at') 
     ->add('finished_at') 
     ->add('default') 
     ->add('deadline') 
     ->add('priority', 'entity', array(
      'class' => 'LanCrmBundle:TaskPriority', 
      'property' => 'title', 
      'multiple' => false 
     )) 
     ->add('project', 'entity', array(
      'class' => 'LanCrmBundle:Project', 
      'property' => 'title', 
      'multiple' => false 
     )) 
     ->add('category', 'entity', array(
      'class' => 'LanCrmBundle:TaskCategory', 
      'property' => 'title', 
      'multiple' => false 
     )) 
     ->add('user', 'entity', array(
      'class' => 'LanSecurityBundle:User', 
      'property' => 'username', 
      'multiple' => false 
     )) 
     ->add('products', 'entity', array(
      'class' => 'LanCrmBundle:Product', 
      'property' => 'title', 
      'multiple' => true 
     )) 
    ; 
} 

回答

0

使用attr屬性在FormBuilder

'attr' => array('class'=>'selectpicker show-tick', 'data-size'=>'auto') 
1

在表單buider中使用attr屬性:

<?php 

namespace Foo\BarBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 

class FooType extends AbstractType 
{ 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('foo', 'choice', array(
       'choices' => array(0 => 'Option 1', 1 => 'Option 2'), 
       'attr' => array('class' => 'selectpicker show-tick', 'data-size' => 'auto'), 
      )) 
     ; 
    } 
} 

編輯

如果你有很多的形式就像你在您的評論說,你可以使用form customization

創建一個新的模板文件:

{# src/Foo/BarBundle/Resources/views/Form/fields.html.twig #} 
{% block choice_widget_collapsed %} 
{% spaceless %} 
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %} class="selectpicker show-tick" data-size="auto"> 
     {% if empty_value is not none %} 
      <option value="">{{ empty_value|trans({}, translation_domain) }}</option> 
     {% endif %} 
     {% if preferred_choices|length > 0 %} 
      {% set options = preferred_choices %} 
      {{ block('choice_widget_options') }} 
      {% if choices|length > 0 and separator is not none %} 
       <option disabled="disabled">{{ separator }}</option> 
      {% endif %} 
     {% endif %} 
     {% set options = choices %} 
     {{ block('choice_widget_options') }} 
    </select> 
{% endspaceless %} 
{% endblock choice_widget_collapsed %} 

,然後導入您的應用程序配置中您的模板:

# app/config/config.yml 
twig: 
    form: 
     resources: 
      - 'FooBarBundle:Form:fields.html.twig' 

的變化將是有效的在所有select

+0

我知道這種方法,但它太費時。我有很多表單。我看到了一些關於創建** fields.html.twig **文件和自定義每個特定輸入類型或只是一個(我的情況)。但我找不到很好的例子。 – user3064931

+0

好的,我編輯了我的答案。 – ncrocfer

+0

這真的很奇怪,它不像我預期的那樣工作。它只適用於時間和日期時間,而不是我的select和multi從一個formtype類型的實體作爲第二參數 - > add('','entity',...) – user3064931

相關問題