views:

241

answers:

2

I am using Symfony 1.3.6 on Ubuntu 10.0.4.

I am using the sfWidgetFormSelectRadio to allow a user to select a picture from a list, in a form.

In the action, the pictures are set up like this:

    $this->form->setWidget('chosenpic', new sfWidgetFormSelectRadio(array(
                            'choices' => $this->pictures,
                            'default' => $this->pictures[count($this->pictures)-1] ))
                            );

In the template, the widget is displayed like this:

<?php echo $form['chosenpic']->render(array('id'=>'check'.($i+1), 'value'=> ($i+1), 'width' => "80", 'height' => "80")); ?>

This generates the following output:

<ul class="radio_list"><li><input type="radio" width="80" height="80" id="check1" value="1" name="flowers[chosenpic]">&nbsp;<label for="flowers_chosenpic_0">http://example.com/media/images/48408000/jpg/_48408794_009829449-1.jpg&lt;/label&gt;&lt;/li&gt;&lt;/ul&gt;

I do no want the the label for appearing, as it messes up the form. Short of manually generating the HTML myself (using the form and widget names, is there a way that I can prevent the widget from displaying a 'label for' ?

+1  A: 

You need to provide a custom formatter. In your form's configure function, do the following:

public function configure()
{
  $this->setWidget('chosenpic', new sfWidgetFormSelectRadio(array(
        'formatter' => array($this, 'pictureRadioFormatterCallback')
  )));
}

Then, add a formatter that doesn't render labels to your form:

public function pictureRadioFormatterCallback($widget, $inputs)
{
  $rows = array();
  foreach ($inputs as $input)
  {
    $rows[] = $widget->renderContentTag('li', $input['input']);
  }

  return !$rows ? 
    '' : 
    $widget->renderContentTag('ul', implode($widget->getOption('separator'), $rows), array('class' => $widget->getOption('class')));
}

However, ideally, you should be using the labels to display the images, rather than displaying the images outside of the labels. Also, it's typically a bad idea to be configuring widgets inside of an action, can that code go in it's own form?

jeremy
Make a custom form (e.g. FrontendModelForm) and then pass the pictures into the form as an option. Or have the form itself perform whatever logic is necessary to retrieve the pictures.
jeremy
A: 

You can do the above -- or use CSS to hide the label generated by default ... something along the lines of:

ul.radio_list li input label { display: none; }

the form itself probably has an #id to help you better specify the CSS selector better. But if you like adding boilerplate PHP code to your app feel free :)

tenken9z