PHP Class yii\validators\CompareValidator

The value being compared with can be another attribute value (specified via [[compareAttribute]]) or a constant (specified via [[compareValue]]. When both are specified, the latter takes precedence. If neither is specified, the attribute will be compared with another attribute whose name is by appending "_repeat" to the source attribute name. 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.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends Validator
ファイルを表示 Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$compareAttribute 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, it assumes the comparison is against another attribute whose name is formed by appending '_repeat' to the attribute being validated. For example, if 'password' is being validated, then the attribute to be compared would be 'password_repeat'.
$compareValue the constant value to be compared with. When both this property and [[compareAttribute]] are set, this property takes precedence.
$message the user-defined error message. It may contain the following placeholders which will be replaced accordingly by the validator: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated - {compareValue}: the value or the attribute label to be compared with - {compareAttribute}: the label of the attribute to be compared with - {compareValueOrAttribute}: the value or the attribute label to be compared with
$operator the operator for comparison. The following operators are supported: - ==: check if two values are equal. The comparison is done is non-strict mode. - ===: check if two values are equal. The comparison is done is strict mode. - !=: check if two values are NOT equal. The comparison is done is non-strict mode. - !==: check if two values are NOT equal. The comparison is done is strict mode. - >: check if value being validated is greater than the value being compared with. - >=: 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. When you want to compare numbers, make sure to also set [[type]] to number.
$type the type of the values being compared. The follow types are supported: - [[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 Methods

Method Description
clientValidateAttribute ( $model, $attribute, $view )
init ( )
validateAttribute ( $model, $attribute )

Protected Methods

Method Description
compareValues ( string $operator, string $type, mixed $value, mixed $compareValue ) : boolean Compares two values with the specified operator.
validateValue ( $value )

Method Details

clientValidateAttribute() public method

public clientValidateAttribute ( $model, $attribute, $view )

compareValues() protected method

Compares two values with the specified operator.
protected compareValues ( string $operator, string $type, mixed $value, mixed $compareValue ) : boolean
$operator string the comparison operator
$type string the type of the values being compared
$value mixed the value being compared
$compareValue mixed another value being compared
return boolean whether the comparison using the specified operator is true.

init() public method

public init ( )

validateAttribute() public method

public validateAttribute ( $model, $attribute )

validateValue() protected method

protected validateValue ( $value )

Property Details

$compareAttribute public_oe property

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, it assumes the comparison is against another attribute whose name is formed by appending '_repeat' to the attribute being validated. For example, if 'password' is being validated, then the attribute to be compared would be 'password_repeat'.
See also: compareValue
public $compareAttribute

$compareValue public_oe property

the constant value to be compared with. When both this property and [[compareAttribute]] are set, this property takes precedence.
See also: compareAttribute
public $compareValue

$message public_oe property

the user-defined error message. It may contain the following placeholders which will be replaced accordingly by the validator: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated - {compareValue}: the value or the attribute label to be compared with - {compareAttribute}: the label of the attribute to be compared with - {compareValueOrAttribute}: the value or the attribute label to be compared with
public $message

$operator public_oe property

the operator for comparison. The following operators are supported: - ==: check if two values are equal. The comparison is done is non-strict mode. - ===: check if two values are equal. The comparison is done is strict mode. - !=: check if two values are NOT equal. The comparison is done is non-strict mode. - !==: check if two values are NOT equal. The comparison is done is strict mode. - >: check if value being validated is greater than the value being compared with. - >=: 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. When you want to compare numbers, make sure to also set [[type]] to number.
public $operator

$type public_oe property

the type of the values being compared. The follow types are supported: - [[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