diff --git a/docs/guide/tutorial-core-validators.md b/docs/guide/tutorial-core-validators.md index e895c4cb2a..cae024a9ba 100644 --- a/docs/guide/tutorial-core-validators.md +++ b/docs/guide/tutorial-core-validators.md @@ -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 diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 2c599da4dc..150a0b2c07 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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 ----------------------- diff --git a/framework/validators/CompareValidator.php b/framework/validators/CompareValidator.php index 5ed17341ff..f13aade3e8 100644 --- a/framework/validators/CompareValidator.php +++ b/framework/validators/CompareValidator.php @@ -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 * @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 {