1. sfWidgetFormChoice.class.php
  2. /** * sfWidgetFormChoice represents a choice widget. * * @package symfony * @subpackage widget * @author Fabien Potencier * @version SVN: $Id: sfWidgetFormChoice.class.php 23994 2009-11-15 22:55:24Z bschussek $ */
  3. class sfWidgetFormChoice extends sfWidgetFormChoiceBase
  4. {
  5. /**
  6. * Constructor.
  7. *
  8. * Available options:
  9. *
  10. * * choices: An array of possible choices (required)
  11. * * multiple: true if the select tag must allow multiple selections
  12. * * expanded: true to display an expanded widget
  13. * if expanded is false, then the widget will be a select
  14. * if expanded is true and multiple is false, then the widget will be a list of radio
  15. * if expanded is true and multiple is true, then the widget will be a list of checkbox
  16. * * renderer_class: The class to use instead of the default ones
  17. * * renderer_options: The options to pass to the renderer constructor
  18. * * renderer: A renderer widget (overrides the expanded and renderer_options options)
  19. * The choices option must be: new sfCallable($thisWidgetInstance, 'getChoices')
  20. * @param array $options An array of options
  21. * @param array $attributes An array of default HTML attributes
  22. *
  23. * @see sfWidgetFormChoiceBase
  24. */
  25. protected function configure($options = array(), $attributes = array())
  26. {
  27. parent::configure($options, $attributes);
  28. $this->addOption('multiple', false);
  29. $this->addOption('expanded', false);
  30. $this->addOption('renderer_class', false);
  31. $this->addOption('renderer_options', array());
  32. $this->addOption('renderer', false);
  33. }
  34. /**
  35. * Sets the format for HTML id attributes. This is made avaiable to the renderer,
  36. * as this widget does not render itself, but delegates to the renderer instead.
  37. *
  38. * @param string $format The format string (must contain a %s for the id placeholder)
  39. *
  40. * @see sfWidgetForm
  41. */
  42. public function setIdFormat($format)
  43. {
  44. $this->options['renderer_options']['id_format'] = $format;
  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 ($this->getOption('multiple'))
  59. {
  60. $attributes['multiple'] = 'multiple';
  61. if ('[]' != substr($name, -2))
  62. {
  63. $name .= '[]';
  64. }
  65. }
  66. if (!$this->getOption('renderer') && !$this->getOption('renderer_class') && $this->getOption('expanded'))
  67. {
  68. unset($attributes['multiple']);
  69. }
  70. return $this->getRenderer()->render($name, $value, $attributes, $errors);
  71. }
  72. /**
  73. * Gets the stylesheet paths associated with the widget.
  74. *
  75. * @return array An array of stylesheet paths
  76. */
  77. public function getStylesheets()
  78. {
  79. return $this->getRenderer()->getStylesheets();
  80. }
  81. /**
  82. * Gets the JavaScript paths associated with the widget.
  83. *
  84. * @return array An array of JavaScript paths
  85. */
  86. public function getJavaScripts()
  87. {
  88. return $this->getRenderer()->getJavaScripts();
  89. }
  90. public function getRenderer()
  91. {
  92. if ($this->getOption('renderer'))
  93. {
  94. return $this->getOption('renderer');
  95. }
  96. if (!$class = $this->getOption('renderer_class'))
  97. {
  98. $type = !$this->getOption('expanded') ? '' : ($this->getOption('multiple') ? 'checkbox' : 'radio');
  99. $class = sprintf('sfWidgetFormSelect%s', ucfirst($type));
  100. }
  101. return new $class(array_merge(array('choices' => new sfCallable(array($this, 'getChoices'))), $this->options['renderer_options']), $this->getAttributes());
  102. }
  103. }

Debug toolbar