1. sfWidgetFormSelectRadio.class.php
  2. /** * sfWidgetFormSelectRadio represents radio HTML tags. * * @package symfony * @subpackage widget * @author Fabien Potencier * @version SVN: $Id: sfWidgetFormSelectRadio.class.php 23994 2009-11-15 22:55:24Z bschussek $ */
  3. class sfWidgetFormSelectRadio extends sfWidgetFormChoiceBase
  4. {
  5. /**
  6. * Constructor.
  7. *
  8. * Available options:
  9. *
  10. * * choices: An array of possible choices (required)
  11. * * label_separator: The separator to use between the input radio and the label
  12. * * separator: The separator to use between each input radio
  13. * * class: The class to use for the main <ul> tag
  14. * * formatter: A callable to call to format the radio choices
  15. * The formatter callable receives the widget and the array of inputs as arguments
  16. * * template: The template to use when grouping option in groups (%group% %options%)
  17. *
  18. * @param array $options An array of options
  19. * @param array $attributes An array of default HTML attributes
  20. *
  21. * @see sfWidgetFormChoiceBase
  22. */
  23. protected function configure($options = array(), $attributes = array())
  24. {
  25. parent::configure($options, $attributes);
  26. $this->addOption('class', 'radio_list');
  27. $this->addOption('label_separator', '&nbsp;');
  28. $this->addOption('separator', "\n");
  29. $this->addOption('formatter', array($this, 'formatter'));
  30. $this->addOption('template', '%group% %options%');
  31. }
  32. /**
  33. * @param string $name The element name
  34. * @param string $value The value selected in this widget
  35. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  36. * @param array $errors An array of errors for the field
  37. *
  38. * @return string An HTML tag string
  39. *
  40. * @see sfWidgetForm
  41. */
  42. public function render($name, $value = null, $attributes = array(), $errors = array())
  43. {
  44. if ('[]' != substr($name, -2))
  45. {
  46. $name .= '[]';
  47. }
  48. $choices = $this->getChoices();
  49. // with groups?
  50. if (count($choices) && is_array(next($choices)))
  51. {
  52. $parts = array();
  53. foreach ($choices as $key => $option)
  54. {
  55. $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
  56. }
  57. return implode("\n", $parts);
  58. }
  59. else
  60. {
  61. return $this->formatChoices($name, $value, $choices, $attributes);
  62. }
  63. }
  64. protected function formatChoices($name, $value, $choices, $attributes)
  65. {
  66. $inputs = array();
  67. foreach ($choices as $key => $option)
  68. {
  69. $baseAttributes = array(
  70. 'name' => substr($name, 0, -2),
  71. 'type' => 'radio',
  72. 'value' => self::escapeOnce($key),
  73. 'id' => $id = $this->generateId($name, self::escapeOnce($key)),
  74. );
  75. if (strval($key) == strval($value === false ? 0 : $value))
  76. {
  77. $baseAttributes['checked'] = 'checked';
  78. }
  79. $inputs[$id] = array(
  80. 'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
  81. 'label' => $this->renderContentTag('label', $option, array('for' => $id)),
  82. );
  83. }
  84. return call_user_func($this->getOption('formatter'), $this, $inputs);
  85. }
  86. public function formatter($widget, $inputs)
  87. {
  88. $rows = array();
  89. foreach ($inputs as $input)
  90. {
  91. $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
  92. }
  93. return !$rows ? '' : $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
  94. }
  95. }

Debug toolbar