Development

/branches/1.1/lib/widget/sfWidgetFormSelectRadio.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/widget/sfWidgetFormSelectRadio.class.php

Revision 17068, 3.4 kB (checked in by fabien, 6 years ago)

[1.1, 1.2, 1.3] fixed clone() in all choice widgets (closes #4417)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfWidgetFormSelectRadio represents radio HTML tags.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfWidgetFormSelectRadio extends sfWidgetForm
20 {
21   /**
22    * Constructor.
23    *
24    * Available options:
25    *
26    *  * choices:         An array of possible choices (required)
27    *  * label_separator: The separator to use between the input radio and the label
28    *  * separator:       The separator to use between each input radio
29    *  * formatter:       A callable to call to format the radio choices
30    *                     The formatter callable receives the widget and the array of inputs as arguments
31    *
32    * @param array $options     An array of options
33    * @param array $attributes  An array of default HTML attributes
34    *   
35    * @see sfWidgetForm
36    */
37   protected function configure($options = array(), $attributes = array())
38   {
39     $this->addRequiredOption('choices');
40
41     $this->addOption('label_separator', '&nbsp;');
42     $this->addOption('separator', "\n");
43     $this->addOption('formatter', array($this, 'formatter'));
44   }
45
46   /**
47    * @param  string $name        The element name
48    * @param  string $value       The value selected in this widget
49    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
50    * @param  array  $errors      An array of errors for the field
51    *
52    * @return string An HTML tag string
53    *
54    * @see sfWidgetForm
55    */
56   public function render($name, $value = null, $attributes = array(), $errors = array())
57   {
58     if ('[]' != substr($name, -2))
59     {
60       $name .= '[]';
61     }
62
63     $choices = $this->getOption('choices');
64     if ($choices instanceof sfCallable)
65     {
66       $choices = $choices->call();
67     }
68
69     $inputs = array();
70     foreach ($choices as $key => $option)
71     {
72       $baseAttributes = array(
73         'name'  => substr($name, 0, -2),
74         'type'  => 'radio',
75         'value' => self::escapeOnce($key),
76         'id'    => $id = $this->generateId($name, self::escapeOnce($key)),
77       );
78
79       if (strval($key) == strval($value === false ? 0 : $value))
80       {
81         $baseAttributes['checked'] = 'checked';
82       }
83
84       $inputs[] = array(
85         'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
86         'label' => $this->renderContentTag('label', $option, array('for' => $id)),
87       );
88     }
89
90     return call_user_func($this->getOption('formatter'), $this, $inputs);
91   }
92
93   public function formatter($widget, $inputs)
94   {
95     $rows = array();
96     foreach ($inputs as $input)
97     {
98       $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
99     }
100
101     return $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => 'radio_list'));
102   }
103
104   public function __clone()
105   {
106     if ($this->getOption('choices') instanceof sfCallable)
107     {
108       $callable = $this->getOption('choices')->getCallable();
109       $class = __CLASS__;
110       if (is_array($callable) && $callable[0] instanceof $class)
111       {
112         $callable[0] = $this;
113         $this->setOption('choices', new sfCallable($callable));
114       }
115     }
116   }
117 }
118
Note: See TracBrowser for help on using the browser.