wpWidgetFormInputCheckbox.class.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. class wpWidgetFormInputCheckbox extends sfWidgetFormInput {
  3. /**
  4. * Constructor.
  5. *
  6. * Available options:
  7. *
  8. * - value_attribute_value: The "value" attribute value to set for the checkbox
  9. *
  10. * @param array $options An array of options
  11. * @param array $attributes An array of default HTML attributes
  12. * @see sfWidgetFormInput
  13. */
  14. public function __construct($options = array(), $attributes = array()) {
  15. $this->addOption('value_attribute_value');
  16. parent::__construct($options, $attributes);
  17. }
  18. /**
  19. *
  20. * @param array $options An array of options
  21. * @param array $attributes An array of default HTML attributes
  22. * @see sfWidgetFormInput
  23. */
  24. protected function configure($options = array(), $attributes = array()) {
  25. parent::configure($options, $attributes);
  26. $this->setOption('type', 'checkbox');
  27. if (isset($attributes['value'])) {
  28. $this->setOption('value_attribute_value', $attributes['value']);
  29. }
  30. }
  31. /**
  32. *
  33. * @param string $name The element name
  34. * @param string $value The this widget is checked if value is not null
  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. * @return string An HTML tag string
  38. * @see sfWidgetForm
  39. */
  40. public function render($name, $value = null, $attributes = array(), $errors = array()) {
  41. if (!empty($this->options['default'])) {
  42. $attributes['checked'] = 'checked';
  43. }
  44. if (!isset($attributes['value']) && !is_null($this->getOption('value_attribute_value'))) {
  45. $attributes['value'] = $this->getOption('value_attribute_value');
  46. }
  47. return parent::render($name, null, $attributes, $errors);
  48. }
  49. }