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

@ -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 {