Added constants for CompareValidator::$type

also improved and fixed docs about 'compare' validator.

fixes #12864
This commit is contained in:
Carsten Brandt
2016-10-28 10:17:27 +02:00
parent d3c7accc49
commit 5f821feff7
3 changed files with 30 additions and 6 deletions

View File

@ -67,8 +67,11 @@ to make sure an input is the same as the verification code displayed by [[yii\ca
// validates if the value of "password" attribute equals to that of "password_repeat"
['password', 'compare'],
// same as above but with explicitly specifying the attribute to compare with
['password', 'compare', 'compareAttribute' => 'password_repeat'],
// validates if age is greater than or equal to 30
['age', 'compare', 'compareValue' => 30, 'operator' => '>='],
['age', 'compare', 'compareValue' => 30, 'operator' => '>=', 'type' => 'number'],
]
```
@ -91,7 +94,9 @@ is as specified by the `operator` property.
* `>=`: check if value being validated is greater than or equal to the value being compared with.
* `<`: check if value being validated is less than the value being compared with.
* `<=`: check if value being validated is less than or equal to the value being compared with.
- `type`: The default comparison type is '[[yii\validators\CompareValidator::TYPE_STRING|string]]', which means the values are
compared byte by byte. When comparing numbers, make sure to set the [[yii\validators\CompareValidator::$type|$type]]
to '[[yii\validators\CompareValidator::TYPE_NUMBER|number]]' to enable numeric comparison.
### Comparing date values

View File

@ -15,6 +15,8 @@ Yii Framework 2 Change Log
- Bug #12824: Enabled usage of `yii\mutex\FileMutex` on Windows systems (davidsonalencar)
- Enh #11037 yii.js and yii.validation.js should use Regexp.test instead of String.match (arogachev, nkovacs)
- Bug #9796: Initialization of not existing `yii\grid\ActionColumn` default buttons (arogachev)
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
2.0.10 October 20, 2016
-----------------------

View File

@ -24,11 +24,28 @@ use yii\helpers\Html;
* CompareValidator supports different comparison operators, specified
* via the [[operator]] property.
*
* The default comparison function is based on string values, which means the values
* are compared byte by byte. When comparing numbers, make sure to set the [[$type]]
* to [[TYPE_NUMBER]] to enable numeric comparison.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CompareValidator extends Validator
{
/**
* Constant for specifying the comparison [[type]] by numeric values.
* @since 2.0.11
* @see type
*/
const TYPE_STRING = 'string';
/**
* Constant for specifying the comparison [[type]] by numeric values.
* @since 2.0.11
* @see type
*/
const TYPE_NUMBER = 'number';
/**
* @var string the name of the attribute to be compared with. When both this property
* and [[compareValue]] are set, the latter takes precedence. If neither is set,
@ -47,10 +64,10 @@ class CompareValidator extends Validator
/**
* @var string the type of the values being compared. The follow types are supported:
*
* - string: the values are being compared as strings. No conversion will be done before comparison.
* - number: the values are being compared as numbers. String values will be converted into numbers before comparison.
* - [[TYPE_STRING|string]]: the values are being compared as strings. No conversion will be done before comparison.
* - [[TYPE_NUMBER|number]]: the values are being compared as numbers. String values will be converted into numbers before comparison.
*/
public $type = 'string';
public $type = self::TYPE_STRING;
/**
* @var string the operator for comparison. The following operators are supported:
*
@ -174,7 +191,7 @@ class CompareValidator extends Validator
*/
protected function compareValues($operator, $type, $value, $compareValue)
{
if ($type === 'number') {
if ($type === self::TYPE_NUMBER) {
$value = (float) $value;
$compareValue = (float) $compareValue;
} else {