duanjiebian6712 2014-06-06 12:29
浏览 6
已采纳

Symfony为每个输入字段添加样式

I have a function, in Symfony2, that creates a form. Then I load it using twig and everything works fine.

The problem is that I would like to add style to each label and each input, separately, and I can only add style to the whole form.

private function createEditForm(Client $entity)
{
    $form = $this->createForm(new ClientType(), $entity, array(
        'action' => $this->generateUrl('client_update', array('id' => $entity->getId())),
        'method' => 'PUT',

    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}

Could I do it the way I'm doing it or do I have to rebuild the function so it doesn't create the form dynamically from an array?

Thank you.

  • 写回答

1条回答 默认 最新

  • dqd22496 2014-06-06 12:36
    关注

    You can customize the class rendered for example in your ClientType via something like this:

    $builder->add('name' , 'text' , array( 'attr' => array( 'class' => 'nameInputField' ) ) );
    

    Or you can add/override directly in your twig template as such:

    {{ form_row(form.name, { 'attr': { 'class': 'nameInputField' }} ) }}
    

    For advanced cases and re-usability refer to the form customization tutorial here:

    http://symfony.com/doc/current/cookbook/form/form_customization.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序