2012-08-31 122 views
13

我想要做一個簡單的表單來添加一個名稱和顏色的活動。symfony2自定義表單選擇選項

所以我想打一個清單,色彩的一些數組,現在它正在我有顏色的名稱。
我可以將任何屬性添加到我的選擇標籤:

$form = $this->createFormBuilder($myclass) 
->add('Colors','choice',array('label'=>'select some colors', 
      'multiple'=>true, 
      'choices'=>array(1=>'red', 2=>'blue', 3=>'green'), 
      'attr'=>array('style'=>'width:300px', 'customattr'=>'customdata') 
      )); 

輸出將是這樣的:

<select name="select" style="width: 300px;" multiple="multiple" customattr="customdata"> 
    <option value="1">red</option> 
    <option value="2">blue</option> 
    <option value="3">green</option> 
</select> 

但我怎麼可以添加selected="selected"和我選擇的選擇我想要的任何屬性?像這樣:

<select name="select" style="width: 300px;" multiple="multiple" customattr="customdata"> 
    <option style="background-color: #F00;" value="1" selected="selected">red</option> 
    <option style="background-color: #00F;" value="2" selected="selected">blue</option> 
    <option style="background-color: #0F0;" value="3">green</option> 
</select> 

我的問題是:我怎麼可以添加自定義ATTR爲option標籤(不適用於select標籤)由symfony的FormBuilder。
通知:我不想使用JavaScript。我想使用symfony2 FormBuilder來自定義我的選擇選項。

回答

10

通常,字段的默認數據由存儲在您的對象中的值決定。例如,如果

class MyClass 
{ 
    private $Colors = array(1, 2); 
} 

則條目「1」和「2」(有標籤「紅色」和「綠色」)默認爲選擇將被顯示。

$myObject->Colors = array(1, 2); 

$form = $this->createFormBuilder($myObject) 
    ... 

最後一種可能性是通過將「數據」選項來覆蓋存儲在對象的默認值:

$builder->add('Colors', 'choice', array(
    'label' => 'select some colors', 
    'multiple' => true, 
    'choices' => array(1 => 'red', 2 => 'blue', 3 => 'green'), 
    'attr' => array('style' => 'width:300px', 'customattr' => 'customdata'), 
    'data' => array(1, 2), 
)); 
1

symfony中的每個Field都繼承自abstract field type,它有一個data選項,您可以在其中傳遞默認選項。

順便說一句,不傳style東西,定製ATTRS使用data-*屬性。

+0

我如何使用'數據'屬性?我如何將它傳遞給FormBuilder。請詳細解釋。 –

+0

只需將'customattr'重命名爲'data-customattr',即可符合HTML標準。 – moonwave99

+0

我想爲''option'標籤使用'customattr',而不是'select'標籤。我怎樣才能通過FormBuilder做到這一點? –

3
你也可以在對象該值將它傳遞給表單之前存儲

使用如本文所述選擇的FO =「選擇」數據選項: http://symfony.com/doc/current/reference/forms/types/field.html

在烏爾情況下可能是這樣

$form = $this->createFormBuilder($myclass) 
->add('Colors','choice',array('label'=>'select some colors', 
      'multiple'=>true, 
      'choices'=>array(1=>'red', 2=>'blue', 3=>'green'), 
      'attr'=>array('style'=>'width:300px', 'customattr'=>'customdata'), 
      'data'=> 1 
      )); 

新元件是數據設置的選擇陣列的數量作爲選擇的屬性

1

中添加類方法的Symfony \分量\表格\ AbstractType :: finishView

public function finishView(FormView $view, FormInterface $form, array $options) 
{ 
    parent::finishView($view, $form, $options); 

    $additionalAttributes = array(); 

    // myMethod - method $choice, returns a value that is to be substituted into the attribute 
    foreach ($view->children['orders']->vars['choices'] as $id => $choice) { 
     $additionalAttributes[$id] = array(
      'data-cost' => $this->propertyAccessor->getValue($choice->data, 'myMethod'), 
      'disabled' => 'disabled', 
     ); 
    } 

    foreach ($view->children['orders']->children as $id => $child) { 
     $child->vars['attr'] = array_replace(
      isset($child->vars['attr']) ? $child->vars['attr'] : array(), 
      $additionalAttributes[$id] 
     ); 
    } 
} 

Symfony2 Form – add attribute tag option in select field type

1

爲了添加根據選擇的值,選擇自定義屬性,考慮使用ChoiceType字段的choice_attr選項。

例如,如果你想JSON編碼實體傳遞給選項,你可以使用這樣的事情:

'choice_attr' => function($val, $key) { 
    return ['data-object' => json_encode($val)]; 
},