Development

/branches/1.3/lib/widget/sfWidgetFormChoice.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/widget/sfWidgetFormChoice.class.php

Revision 30762, 4.1 kB (checked in by fabien, 4 years ago)

[1.3, 1.4] fixed phpdoc (closes #8931)

  • 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  * sfWidgetFormChoice represents a choice widget.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfWidgetFormChoice extends sfWidgetFormChoiceBase
20 {
21   /**
22    * Constructor.
23    *
24    * Available options:
25    *
26    *  * multiple:         true if the select tag must allow multiple selections
27    *  * expanded:         true to display an expanded widget
28    *                        if expanded is false, then the widget will be a select
29    *                        if expanded is true and multiple is false, then the widget will be a list of radio
30    *                        if expanded is true and multiple is true, then the widget will be a list of checkbox
31    *  * renderer_class:   The class to use instead of the default ones
32    *  * renderer_options: The options to pass to the renderer constructor
33    *  * renderer:         A renderer widget (overrides the expanded and renderer_options options)
34    *                      The choices option must be: new sfCallable($thisWidgetInstance, 'getChoices')
35    * @param array $options     An array of options
36    * @param array $attributes  An array of default HTML attributes
37    *
38    * @see sfWidgetFormChoiceBase
39    */
40   protected function configure($options = array(), $attributes = array())
41   {
42     parent::configure($options, $attributes);
43
44     $this->addOption('multiple', false);
45     $this->addOption('expanded', false);
46     $this->addOption('renderer_class', false);
47     $this->addOption('renderer_options', array());
48     $this->addOption('renderer', false);
49   }
50
51   /**
52    * Sets the format for HTML id attributes. This is made avaiable to the renderer,
53    * as this widget does not render itself, but delegates to the renderer instead.
54    *
55    * @param string $format  The format string (must contain a %s for the id placeholder)
56    *
57    * @see sfWidgetForm
58    */
59   public function setIdFormat($format)
60   {
61     $this->options['renderer_options']['id_format'] = $format;
62   }
63
64   /**
65    * Renders the widget.
66    *
67    * @param  string $name        The element name
68    * @param  string $value       The value selected in this widget
69    * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
70    * @param  array  $errors      An array of errors for the field
71    *
72    * @return string An HTML tag string
73    *
74    * @see sfWidgetForm
75    */
76   public function render($name, $value = null, $attributes = array(), $errors = array())
77   {
78     if ($this->getOption('multiple'))
79     {
80       $attributes['multiple'] = 'multiple';
81
82       if ('[]' != substr($name, -2))
83       {
84         $name .= '[]';
85       }
86     }
87
88     if (!$this->getOption('renderer') && !$this->getOption('renderer_class') && $this->getOption('expanded'))
89     {
90       unset($attributes['multiple']);
91     }
92
93     return $this->getRenderer()->render($name, $value, $attributes, $errors);
94   }
95
96   /**
97    * Gets the stylesheet paths associated with the widget.
98    *
99    * @return array An array of stylesheet paths
100    */
101   public function getStylesheets()
102   {
103     return $this->getRenderer()->getStylesheets();
104   }
105
106   /**
107    * Gets the JavaScript paths associated with the widget.
108    *
109    * @return array An array of JavaScript paths
110    */
111   public function getJavaScripts()
112   {
113     return $this->getRenderer()->getJavaScripts();
114   }
115
116   public function getRenderer()
117   {
118     if ($this->getOption('renderer'))
119     {
120       return $this->getOption('renderer');
121     }
122
123     if (!$class = $this->getOption('renderer_class'))
124     {
125       $type = !$this->getOption('expanded') ? '' : ($this->getOption('multiple') ? 'checkbox' : 'radio');
126       $class = sprintf('sfWidgetFormSelect%s', ucfirst($type));
127     }
128
129     return new $class(array_merge(array('choices' => new sfCallable(array($this, 'getChoices'))), $this->options['renderer_options']), $this->getAttributes());
130   }
131 }
132
Note: See TracBrowser for help on using the browser.