sfWidgetFormInputCheckbox.class.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWidgetFormInputCheckbox represents an HTML checkbox tag.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormInputCheckbox.class.php 11279 2008-09-01 13:33:04Z nicolas $
  16. */
  17. class sfWidgetFormInputCheckbox extends sfWidgetFormInput
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * Available options:
  23. *
  24. * - value_attribute_value: The "value" attribute value to set for the checkbox
  25. *
  26. * @param array $options An array of options
  27. * @param array $attributes An array of default HTML attributes
  28. *
  29. * @see sfWidgetFormInput
  30. */
  31. public function __construct($options = array(), $attributes = array())
  32. {
  33. $this->addOption('value_attribute_value');
  34. parent::__construct($options, $attributes);
  35. }
  36. /**
  37. * @param array $options An array of options
  38. * @param array $attributes An array of default HTML attributes
  39. *
  40. * @see sfWidgetFormInput
  41. */
  42. protected function configure($options = array(), $attributes = array())
  43. {
  44. parent::configure($options, $attributes);
  45. $this->setOption('type', 'checkbox');
  46. if (isset($attributes['value']))
  47. {
  48. $this->setOption('value_attribute_value', $attributes['value']);
  49. }
  50. }
  51. /**
  52. * @param string $name The element name
  53. * @param string $value The this widget is checked if value is not null
  54. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  55. * @param array $errors An array of errors for the field
  56. *
  57. * @return string An HTML tag string
  58. *
  59. * @see sfWidgetForm
  60. */
  61. public function render($name, $value = null, $attributes = array(), $errors = array())
  62. {
  63. if (!is_null($value) && $value !== false)
  64. {
  65. $attributes['checked'] = 'checked';
  66. }
  67. if (!isset($attributes['value']) && !is_null($this->getOption('value_attribute_value')))
  68. {
  69. $attributes['value'] = $this->getOption('value_attribute_value');
  70. }
  71. return parent::render($name, null, $attributes, $errors);
  72. }
  73. }