|
Revision 11612, 1.7 kB
(checked in by nicolas, 5 years ago)
|
[1.1] fixed sfValidatorChoice and sfValidatorChoiceMany can evaluate strings as int using PHP in_array() function (closes #4212)
|
- Property svn:mime-type set to
text/x-php
- Property svn:eol-style set to
native
- Property svn:keywords set to
Id
|
| Line | |
|---|
| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfValidatorChoice extends sfValidatorBase |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* Configures the current validator. |
|---|
| 23 |
* |
|---|
| 24 |
* Available options: |
|---|
| 25 |
* |
|---|
| 26 |
* * choices: An array of expected values (required) |
|---|
| 27 |
* |
|---|
| 28 |
* @param array $options An array of options |
|---|
| 29 |
* @param array $messages An array of error messages |
|---|
| 30 |
* |
|---|
| 31 |
* @see sfValidatorBase |
|---|
| 32 |
*/ |
|---|
| 33 |
protected function configure($options = array(), $messages = array()) |
|---|
| 34 |
{ |
|---|
| 35 |
$this->addRequiredOption('choices'); |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
* @see sfValidatorBase |
|---|
| 40 |
*/ |
|---|
| 41 |
protected function doClean($value) |
|---|
| 42 |
{ |
|---|
| 43 |
$choices = $this->getOption('choices'); |
|---|
| 44 |
if ($choices instanceof sfCallable) |
|---|
| 45 |
{ |
|---|
| 46 |
$choices = $choices->call(); |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
if (!self::inChoices($value, $choices)) |
|---|
| 50 |
{ |
|---|
| 51 |
throw new sfValidatorError($this, 'invalid', array('value' => $value)); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
return $value; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
* Checks if a value is part of given choices (see bug #4212) |
|---|
| 59 |
* |
|---|
| 60 |
* @param mixed $value The value to check |
|---|
| 61 |
* @param array $choices The array of available choices |
|---|
| 62 |
* |
|---|
| 63 |
* @return Boolean |
|---|
| 64 |
*/ |
|---|
| 65 |
static protected function inChoices($value, array $choices = array()) |
|---|
| 66 |
{ |
|---|
| 67 |
foreach ($choices as $choice) |
|---|
| 68 |
{ |
|---|
| 69 |
if ((string) $choice == (string) $value) |
|---|
| 70 |
{ |
|---|
| 71 |
return true; |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
return false; |
|---|
| 76 |
} |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|