2015-02-08 28 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'; 
    } 
}