【问题标题】:Symfony2 Form CustomizationSymfony2 表单自定义
【发布时间】:2012-10-09 04:59:31
【问题描述】:

我想要这样的东西:

<textarea rows="30" cols="70"  class="TextBox" style="height:100px;">

但在我的 symfony2 应用程序中,而不是在树枝模板中 我试过这个:

        $builder->add('history', 'textarea', array('label' => 'Nome' , 'max_length' => 1048576 , 'rows' = 30 , 'cols' = 70));

但我得到“行”和“列”不是选项...

在树枝上我想要这样的东西:

<label for="history">{{'form_anamnese_history'}}</label>
{{ form_widget(form.history) }}

成为类似论坛帖子的文本框!

【问题讨论】:

  • 调整元素的大小最好在你的 css 中完成。

标签: forms symfony twig


【解决方案1】:

使用attr 数组,如documentation 中所述:

$builder->add('history', 'textarea', array(
    'attr' => array('cols' => '5', 'rows' => '5'),
));

【讨论】:

    【解决方案2】:

    您可以在 Twig 中而不是在表单中设置 textarea 的显示属性:

    {{ form_widget(edit_form.comment, { 'attr': { 
      'style' : 'width:525px', 
      'rows' : '4', 
      'cols' : '30' }} ) }}
    

    如上所述,如果可能的话,最好在 CSS 中设置它。

    【讨论】:

    • 根据我的经验,最好使用行和列,因为它会自动匹配行高。
    • 在 twig 视图中定义 rowscols 不适用于 symfony 2.8。它仅在@juan 指出的表单构建期间有效。
    • 我会说这是最好的答案,因为表单小部件的物理尺寸应该是特定树枝模板的问题,而不是表单类型类本身的问题。
    【解决方案3】:

    Symfony 3中问题的解决方案。

    第一:

    use Symfony\Component\Form\Extension\Core\Type\TextareaType;
    

    第二: 这是Form中的代码:

    ->add('biografia', TextareaType::class, array(
          'label' => 'Como me identifico, Identifiquese utilizando un máximo de 500 caracteres',
          'attr' => array('class' => 'myclass')
          ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多