2015-02-08 33 views
1

我有這個獲取輸入型嫩枝,以確定標籤

{% block form_row %} 
    <div class="form-group"> 
     {{ form_label(form) }} 

     {{ form_widget(form) }} 
    </div> 
{% endblock form_row %} 

其用於覆蓋主枝杈表單字段。

但我需要的標籤是不同的,取決於正在呈現的表單字段的類型。

我怎樣才能在這裏找到,然後打電話給其他人而不是form_label

我基本上希望能夠做到這一點,這是因爲它會出現的標籤出現在複選框的輸入之後,但我想反轉/定製它。

{% block form_row %} 
    <div class="form-group"> 
     {% if(type is checkbox) %} 
      // checkbox label here 
     {% else %} 
      {{ form_label(form) }} 
     {% endif %} 

     {{ form_widget(form) }} 
    </div> 
{% endblock form_row %} 

回答

2

您可以覆蓋用於呈現特定表單類型的塊。

例如,如果你想覆蓋的電子郵件輸入的標籤模板,你應該重寫EMAIL_LABEL塊:

{% block email_label %} 
This is the template used for all email input 
{% endblock %} 

{% block form_label %} 
This is the fallback template for all other input types 
{% endblock %} 

您可以檢查您可以尋找到替代特定形式的圖,該塊form.vars.block_prefixes

例如,對於一個「personnal_email」類型「電子郵件」的領域,它會包含:

array:4 [▼ 
    0 => "form" 
    1 => "text" 
    2 => "email" 
    3 => "_form_personnal_email" 
] 

這意味着你可以重寫塊(從更一般的一個)form_(widget|label|error)text_(widget|label|error)email_(widget|label|error)_form_personnal_email_(widget|label|error)(最後一個用於覆蓋特定字段的渲染)。

它回答你的問題嗎?

UPDATE

這裏是你必須做的:

{% block form_row %} 
    <div class="form-group"> 
     {{ form_label(form) }} 

     {{ form_widget(form) }} 
    </div> 
{% endblock %} 

{% block checkbox_label %} 
    <!-- Your checkbox specific label --> 
{% endblock %} 

你不能在form_row塊訪問type,因爲它是在form_widget的子塊只能定義(見here例如)

+0

有用的答案,幫我發現新的東西。謝謝 – 2015-02-09 00:54:21

+0

看我的編輯,我不知道這是否有幫助。我希望能夠使用該類型並按照我的意願使用它。 – 2015-02-09 09:01:25

+0

您是否嘗試覆蓋checkbox_label塊? – Gildas 2015-02-09 13:17:12

0

您可以使用自定義格式:

<div class="form-group"> 
    {{ form_label(form.your_value, 'Your Title of field', { 'label_attr': {'class': 'col-sm-3 control-label'} }) }} 
     <div class="col-sm-9"> 
      {{ form_widget(form.your_value, { 'attr': {'class': 'form-control selectJS'} }) }} 
     </div> 
</div> 

,或者您可以使用FormType(如果生成的實體,這是表格文件夾文件),喜歡的:

<?php 

namespace Ens\YourBundle\Form; 

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

class NoticeType extends AbstractType 
{ 
    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('title','text', array(
       'label'=>'Title', 
       'label_attr'=>array('class'=>'col-sm-3 control-label'), 
       'attr'=>array('class'=> 'form-control') 
      )) 
      ->add('text', 'textarea', array(
       'label'=>'Text', 
       'label_attr'=>array('class'=>'col-sm-3 control-label'), 
       'attr'=>array('class'=> 'form-control') 
      )) 
      ->add('keep_on_top','checkbox', array(
       'label'=>'Keep on top', 
       'required'=>false 
      )) 
      ->add('start', 'date', array(
       'attr'=>array('class'=> 'hidden') 
      )) 
      ->add('end', 'date', array(
       'attr'=>array('class'=> 'hidden') 
      )) 
     ; 
    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'Ens\YourBundle\Entity\Notice' 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return 'notice'; 
    } 
}