1

我有一个在 Symfony 1.4 中呈现复选框的 sfWidgetFormChoice。我遇到的问题是我想将每个选择作为表格中的一行。所以我的 html 最好是这样的:

<tr><td>Choice 1</td></tr>
<tr><td>Choice 1</td></tr>
<tr><td>Choice 1</td></tr>
            .
            .
            .

这样它将呈现为表格而不是列表。

谢谢!

4

1 回答 1

2

在您的表单MyForm中创建以下小部件:

$this->widgetSchema ['my_widget'] = new sfWidgetFormChoice(array('multiple' => true, 'expanded' => true, 'choices' => my_choices, 'renderer_class' => 'sfWidgetFormSelectCheckbox', 'renderer_options' => array('formatter' => array('MyForm', 'MyFormatter'))));

$this->validatorSchema ['my_widget'] = new sfValidatorChoice(array('multiple' => true, 'choices' => array_keys(my_choices), 'required' => false));

然后添加以下名为的格式化程序方法MyFormatter

public static function MyFormatter($widget, $inputs) {
       $result = '<table>';
       foreach ($inputs as $input) {
          $result .= '<tr><td>' . $input ['label'] . '   ' . $input ['input'] . '</td></tr>';
        }
        $result .= '</table>';
        return $result;
}

我曾经通过在小部件定义中设置renderer_class选项来将选项呈现为复选框,您可以使用它来将其呈现为单选按钮,并且您可以通过更改从另一个表单调用另一个格式化程序。sfWidgetFormSelectCheckboxsfWidgetFormSelectRadioarray('formatter' => array('MyForm', 'MyFormatter'))

于 2011-06-13T08:57:50.827 回答