Juan Besa
Juan Besa

Reputation: 4410

Render each choice as a row in a table in a Symfony choice widget

I have a sfWidgetFormChoice which renders checkboxes in Symfony 1.4. The problem I have is that I want to rend each choice as a row in a table. So my html would ideally be something like this:

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

So that it would render as a table instead of a list.

Thanks!

Upvotes: 1

Views: 2947

Answers (1)

emj8321
emj8321

Reputation: 116

In your form called MyForm create the following widget:

$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));

Then add the following formatter method called 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;
}

I used for choices to be rendered as checkboxes by setting renderer_class option to sfWidgetFormSelectCheckbox in widget definition you can use sfWidgetFormSelectRadio to render it to radio buttons and you can call another formatter from another form by changing array('formatter' => array('MyForm', 'MyFormatter')).

Upvotes: 2

Related Questions