【问题标题】:Symfony 1.4 Form TemplatingSymfony 1.4 表单模板
【发布时间】:2013-05-05 11:13:07
【问题描述】:

我正在安装 Symfony 1.4,我们需要重新皮肤以匹配新的公司品牌。新皮肤的要求之一是单选按钮具有以下语法 -

<label><input name="foo" type="radio" value="bar" id="foo_bar"><span></span></label>

然而,单选按钮的默认 Symfony 代码是 -

<input name="foo" type="radio" value="bar" id="foo_bar">

我可以更改以添加其他元素以围绕输入标签的模板在哪里?我已经通过代码搜索了高低,我似乎无法找到生成此代码的位置。

提前致谢!

【问题讨论】:

    标签: symfony1 symfony-1.4 symfony-forms


    【解决方案1】:

    您需要创建一个自定义小部件类。我扩展了收音机小部件并更改了格式化程序功能。

    您可能需要自己稍微自定义一下。但是下面的myWidgetFormSelectRadio 类会将每个单选字段包装在您指定的标签中。

    // \lib\widget\myWidgetFormSelectRadio.class.php
    class myWidgetFormSelectRadio extends sfWidgetFormSelectRadio
    {
      protected function formatChoices($name, $value, $choices, $attributes)
      {
        $inputs = array();
        foreach ($choices as $key => $option)
        {
          $baseAttributes = array(
            'name'  => substr($name, 0, -2),
            'type'  => 'radio',
            'value' => self::escapeOnce($key),
            'id'    => $id = $this->generateId($name, self::escapeOnce($key)),
          );
    
          if (strval($key) == strval($value === false ? 0 : $value))
          {
            $baseAttributes['checked'] = 'checked';
          }
    
          $inputs[] = array(
            'input' => $this->renderContentTag('label', $this->renderTag('input', array_merge($baseAttributes, $attributes)), array('for' => $id)),
          );
        }
    
        return call_user_func($this->getOption('formatter'), $this, $inputs);
      }
    
      public function formatter($widget, $inputs)
      {
        $rows = array();
        foreach ($inputs as $input)
        {
          $rows[] = $input['input'].$this->getOption('label_separator');
        }
    
        return implode($this->getOption('separator'), $rows);
      }
    }
    

    然后在你的表单类中

    // \lib\form\myForm.class.php
    public function configure()
    {
        $this->widgetSchema['my_field'] = new myWidgetFormSelectRadio(array('choices' => $choices));
    
        // ....
    }
    

    您是否偶然使用了 Twitter Bootstrap?如果是这样,那么您也需要扩展 Symfony 复选框小部件。如果您这样做,那么您可能需要扩展 sfWidgetFormChoice 类并更改 getRenderer() 方法,以便它在呈现单选/复选框字段时使用您的 myWidgetFormSelectRadiomyWidgetFormSelectCheckbox 类。

    // \lib\widget\myWidgetFormChoice.php
    class myWidgetFormChoice extends sfWidgetFormChoice
    {
      public function getRenderer()
      {
        if ($this->getOption('renderer'))
        {
          return $this->getOption('renderer');
        }
    
        $prefix = $this->getOption('use_my_custom_widget') ? 'my' : 'sf';
    
        if (!$class = $this->getOption('renderer_class'))
        {
          $type = !$this->getOption('expanded') ? '' : ($this->getOption('multiple') ? 'checkbox' : 'radio');
          $class = sprintf($prefix . 'WidgetFormSelect%s', ucfirst($type));
        }
    
        return new $class(array_merge(array('choices' => new sfCallable(array($this, 'getChoices'))), $this->options['renderer_options']), $this->getAttributes());
      }
    }
    

    然后在你的表单类中

    public function configure()
    {
        $this->widgetSchema['my_field'] = new myWidgetFormChoice(array(
             'choices' => $choices,
             'expanded' => true,
             'use_my_custom_widget' => true,
        ));
    
        // ....
    }
    

    【讨论】:

      【解决方案2】:

      您是否使用管理生成器在后端生成表单?

      如果是这样,您应该在位于 .../cache/app/enviroment/modules/ 的缓存文件夹中查找表单

      你会注意到会有带有“auto”前缀的文件夹,如果你在开发环境“app_dev”中使用应用程序,这些文件夹是symfony自动生成的以缓存模块。

      然后在 .../cache/backend/dev/modules/autoModule/templates/ 上查找需要更改视图的模块

      您可以在此link 中找到位于模板自定义下的每个文件的简要说明。

      您需要将所需的文件复制到 .../apps/backend/module/templates/ 文件夹中,您可以创建模板文件夹。请注意,如果您更改模块的 config.yml,更改将应用​​到缓存中,因此您必须再次更新模板(从位于缓存中的模板复制并粘贴到您的模块/模板)

      现在困难的是,我发现我可以用 javascript 以更快速和简单的方式修改表单 HTML,这就是我之前在一些 symfony 项目中所做的,希望对您有所帮助。

      【讨论】:

        【解决方案3】:

        Puh,Symfony 1. 很久以前我就开始工作了。我挖掘了旧的 symfony 文档,custom widget 可能是你想要的。我不认为,symfony 1 有 symfony 2 那样的形式主题。

        【讨论】:

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