3

私は Symfony 1.4 のインストールに取り組んでおり、新しい会社のブランドに合わせてスキンを変更する必要がありました。新しいスキンの要件の 1 つは、ラジオ ボタンの構文が次のとおりであることです。

<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">

入力タグを囲む追加要素を追加するために変更できるテンプレートはどこにありますか? コード全体を隅から隅まで検索しましたが、このコードが生成されている場所を見つけることができないようです。

前もって感謝します!

4

3 に答える 3

7

カスタム ウィジェット クラスを作成する必要があります。ラジオ ウィジェットを拡張し、フォーマッタ機能を変更しました。

これを自分で少しカスタマイズする必要があるかもしれません。ただし、以下の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()メソッドを変更し、ラジオ/チェックボックス フィールドをレンダリングするときにmyWidgetFormSelectRadioandクラスを使用するようにします。myWidgetFormSelectCheckbox

// \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,
    ));

    // ....
}
于 2013-05-10T19:58:09.070 に答える