English 中文(简体)
How to change the behavior of sfWidgetFormSelectRadio in symfony?
原标题:
new sfWidgetFormSelectRadio(
                array( choices  => $images)));

The above will render each option something like:

<input type="radio" name="name" value="image_path">

How to make it render this way with minimal code:

<input type="radio" name="name" value="image_path"><img src="image_path" />
最佳回答

This is untested and straight from me reading the Symfony API docs for that widget. You ll need to extend the sfWidgetFormSelectRadio class, call it something like myWidgetFormSelectRadio and stick it in lib/widget/myWidgetFormSelectRadio.class.php in your project.

Override the formatChoices() method like so:

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[$id] = array(
         input  =>
          $this->renderTag( input , array_merge($baseAttributes, $attributes))
          . $this->renderTag( img , array( src  => self::escapeOnce($key))),
         label  => $this->renderContentTag( label , self::escapeOnce($option), array( for  => $id)),
      );
    }

    return call_user_func($this->getOption( formatter ), $this, $inputs);
  }
}

so you re basically appending the img tag to the input.

In your form s configure() method you ll then need to switch from using sfWidgetFormSelectRadio to using myWidgetFormSelectRadio to use the new widget.

Let me know if this works ;-)

问题回答

暂无回答




相关问题
Eclipse formatter to keep One-Liners

Can Eclipse Formatter be configured to keep: public Long getId() { return this.id; } And maybe to format small (one line) definitions as one-liners?

Free SQL formatter tool [closed]

is there any free SQL formatter tool? I am using http://www.sqlinform.com/ for small queries. It is very good. But free version supports only 100 lines. TORA has the feature but it has many issues and ...

Zend Studio code formatter problem with comments

i have Zend Studio 7.x, mostly use it for Zend Framework PHP and OOP JavaScript. When i run formatter on *.php file, often space is added after block of inline comments Before formatter run: <?...

Objective c string formatter for distances

I have a distance as a float and I m looking for a way to format it nicely for human readers. Ideally, I d like it to change from m to km as it gets bigger, and to round the number nicely. Converting ...

AutoMapper - Adding Custom Formatters

I am using AutoMapper 1.0 RTW and adding a couple of custom formatters: Mapper.AddFormatter<AlertTypeFormatter>(); Mapper.AddFormatter<DateStringFormatter>(); The destination ...

Smart Java formatter

I m looking for a smart Java String formatter similar to the standard Formatter: StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer); formatter.format("...

Understanding the $ in Java s format strings

StringBuilder sb = new StringBuilder(); // Send all output to the Appendable object sb Formatter formatter = new Formatter(sb, Locale.US); // Explicit argument indices may be used to re-order ...

热门标签