1. sfValidatorChoice.class.php
  2. /** * sfValidatorChoice validates than the value is one of the expected values. * * @package symfony * @subpackage validator * @author Fabien Potencier * @version SVN: $Id: sfValidatorChoice.class.php 22264 2009-09-23 05:54:32Z fabien $ */
  3. class sfValidatorChoice extends sfValidatorBase
  4. {
  5. /**
  6. * Configures the current validator.
  7. *
  8. * Available options:
  9. *
  10. * * choices: An array of expected values (required)
  11. * * multiple: true if the select tag must allow multiple selections
  12. * * min: The minimum number of values that need to be selected (this option is only active if multiple is true)
  13. * * max: The maximum number of values that need to be selected (this option is only active if multiple is true)
  14. *
  15. * @param array $options An array of options
  16. * @param array $messages An array of error messages
  17. *
  18. * @see sfValidatorBase
  19. */
  20. protected function configure($options = array(), $messages = array())
  21. {
  22. $this->addRequiredOption('choices');
  23. $this->addOption('multiple', false);
  24. $this->addOption('min');
  25. $this->addOption('max');
  26. $this->addMessage('min', 'At least %min% values must be selected (%count% values selected).');
  27. $this->addMessage('max', 'At most %max% values must be selected (%count% values selected).');
  28. }
  29. /**
  30. * @see sfValidatorBase
  31. */
  32. protected function doClean($value)
  33. {
  34. $choices = $this->getChoices();
  35. if ($this->getOption('multiple'))
  36. {
  37. $value = $this->cleanMultiple($value, $choices);
  38. }
  39. else
  40. {
  41. if (!self::inChoices($value, $choices))
  42. {
  43. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  44. }
  45. }
  46. return $value;
  47. }
  48. public function getChoices()
  49. {
  50. $choices = $this->getOption('choices');
  51. if ($choices instanceof sfCallable)
  52. {
  53. $choices = $choices->call();
  54. }
  55. return $choices;
  56. }
  57. /**
  58. * Cleans a value when multiple is true.
  59. *
  60. * @param mixed $value The submitted value
  61. *
  62. * @return array The cleaned value
  63. */
  64. protected function cleanMultiple($value, $choices)
  65. {
  66. if (!is_array($value))
  67. {
  68. $value = array($value);
  69. }
  70. foreach ($value as $v)
  71. {
  72. if (!self::inChoices($v, $choices))
  73. {
  74. throw new sfValidatorError($this, 'invalid', array('value' => $v));
  75. }
  76. }
  77. $count = count($value);
  78. if ($this->hasOption('min') && $count < $this->getOption('min'))
  79. {
  80. throw new sfValidatorError($this, 'min', array('count' => $count, 'min' => $this->getOption('min')));
  81. }
  82. if ($this->hasOption('max') && $count > $this->getOption('max'))
  83. {
  84. throw new sfValidatorError($this, 'max', array('count' => $count, 'max' => $this->getOption('max')));
  85. }
  86. return $value;
  87. }
  88. /**
  89. * Checks if a value is part of given choices (see bug #4212)
  90. *
  91. * @param mixed $value The value to check
  92. * @param array $choices The array of available choices
  93. *
  94. * @return Boolean
  95. */
  96. static protected function inChoices($value, array $choices = array())
  97. {
  98. foreach ($choices as $choice)
  99. {
  100. if ((string) $choice == (string) $value)
  101. {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. }

Debug toolbar