代码之家  ›  专栏  ›  技术社区  ›  Tom

为symfony表单标签定制CSS类?

  •  2
  • Tom  · 技术社区  · 15 年前

    有没有一种简单的方法来声明symfony表单标签的css类?

    这不起作用:

    <?php echo $form['email']->renderLabel('class' => 'my-css') ?>
    

    发现了这一点,它可以工作,但感觉不太直观,因为它要求在模板中写出标签,从而使表单标签本身过时:

    <?php echo $form['email']->renderLabel('This is a label text', array('class' => 'my-css') ?>
    

    有人知道更好的方法吗?

    谢谢

    2 回复  |  直到 15 年前
        1
  •  7
  •   nortron    15 年前

    向第一个参数传入空值不会重写标签文本:

    <?php echo $form['email']->renderLabel(null, array('class' => 'my-css')) ?>
    

    http://www.symfony-project.org/api/1_4/sfFormField#method_renderlabel

    $label标签名称(不为空以覆盖当前值)

        2
  •  2
  •   Radu Dragomir    15 年前

    通过创建扩展sfWidgetFormSchemaFormatter的自定义类,可以更改每个表单的格式设置工具。

        class sfWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatter
    {
      protected
        $rowFormat       = "<tr>\n  <th class=\"my-label-class\">%label%</th>\n  <td>%error%%field%%help%%hidden_fields%</td>\n</tr>\n",
        $errorRowFormat  = "<tr><td class=\"my-error-class\" colspan=\"2\">\n%errors%</td></tr>\n",
        $helpFormat      = '<br />%help%',
        $decoratorFormat = "<table>\n  %content%</table>";
    }
    

    然后,可以在Form类中更改格式化程序:

    $this->getWidgetSchema()->setFormFormatterName('custom');
    

    或者,可以为config/projectconfiguration.class.php文件中的每个表单设置新的格式化程序:

    sfWidgetFormSchema::setDefaultFormFormatterName('custom');