Class yii\validators\CompareValidator

Inheritanceyii\validators\CompareValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/validators/CompareValidator.php

CompareValidator compares the specified attribute value with another value.

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.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$attributes array|string Attributes to be validated by this validator. yii\validators\Validator
$builtInValidators array List of built-in validators (name => class or configuration) yii\validators\Validator
$compareAttribute string The name of the attribute to be compared with. yii\validators\CompareValidator
$compareValue mixed The constant value to be compared with. yii\validators\CompareValidator
$enableClientValidation boolean Whether to enable client-side validation for this validator. yii\validators\Validator
$except array|string Scenarios that the validator should not be applied to. yii\validators\Validator
$isEmpty callable A PHP callable that replaces the default implementation of isEmpty(). yii\validators\Validator
$message string The user-defined error message. yii\validators\CompareValidator
$on array|string Scenarios that the validator can be applied to. yii\validators\Validator
$operator string The operator for comparison. yii\validators\CompareValidator
$skipOnEmpty boolean Whether this validation rule should be skipped if the attribute value is null or an empty string. yii\validators\Validator
$skipOnError boolean Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. yii\validators\Validator
$type string The type of the values being compared. yii\validators\CompareValidator
$when callable A PHP callable whose return value determines whether this validator should be applied. yii\validators\Validator
$whenClient string A JavaScript function name whose return value determines whether this validator should be applied on the client-side. yii\validators\Validator

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addError() Adds an error about the specified attribute to the model object. yii\validators\Validator
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
clientValidateAttribute() {@inheritdoc} yii\validators\CompareValidator
createValidator() Creates a validator object. yii\validators\Validator
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getAttributeNames() Returns cleaned attribute names without the ! character at the beginning. yii\validators\Validator
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getClientOptions() {@inheritdoc} yii\validators\CompareValidator
getValidationAttributes() Returns a list of attributes this validator applies to. yii\validators\Validator
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() {@inheritdoc} yii\validators\CompareValidator
isActive() Returns a value indicating whether the validator is active for the given scenario and attribute. yii\validators\Validator
isEmpty() Checks if the given value is empty. yii\validators\Validator
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
trigger() Triggers an event. yii\base\Component
validate() Validates a given value. yii\validators\Validator
validateAttribute() {@inheritdoc} yii\validators\CompareValidator
validateAttributes() Validates the specified object. yii\validators\Validator

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
compareValues() Compares two values with the specified operator. yii\validators\CompareValidator
formatMessage() Formats a mesage using the I18N, or simple strtr if \Yii::$app is not available. yii\validators\Validator
validateValue() {@inheritdoc} yii\validators\CompareValidator

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
TYPE_NUMBER 'number' Constant for specifying the comparison $type by numeric values. yii\validators\CompareValidator
TYPE_STRING 'string' Constant for specifying the comparison $type by numeric values. yii\validators\CompareValidator

Property Details

$compareAttribute public 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 string $compareAttribute null
$compareValue public property

The constant value to be compared with. When both this property and $compareAttribute are set, this property takes precedence.

See also $compareAttribute.

public mixed $compareValue null
$message public 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 string $message null
$operator public 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 string $operator '=='
$type public property

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.
public string $type self::TYPE_STRING

Method Details

clientValidateAttribute() public method

{@inheritdoc}

public void clientValidateAttribute ( $model, $attribute, $view )
$model
$attribute
$view
compareValues() protected method

Compares two values with the specified operator.

protected boolean compareValues ( $operator, $type, $value, $compareValue )
$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.

getClientOptions() public method

{@inheritdoc}

public void getClientOptions ( $model, $attribute )
$model
$attribute
init() public method

{@inheritdoc}

public void init ( )
validateAttribute() public method

{@inheritdoc}

public void validateAttribute ( $model, $attribute )
$model
$attribute
validateValue() protected method

{@inheritdoc}

protected void validateValue ( $value )
$value