CCompareValidator
CCompareValidator compares the specified attribute value with another value and validates if they are equal.
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.
The comparison can be either
strict or not.
Starting from version 1.0.8, CCompareValidator supports different comparison operators.
Previously, it only compares to see if two values are equal or not.
Public Properties
Hide inherited properties
| Property | Type | Description | Defined By |
| allowEmpty |
boolean |
whether the attribute value can be null or empty. |
CCompareValidator |
| attributes |
array |
list of attributes to be validated. |
CValidator |
| builtInValidators |
array |
list of built-in validators (name=>class) |
CValidator |
| compareAttribute |
string |
the name of the attribute to be compared with |
CCompareValidator |
| compareValue |
string |
the constant value to be compared with |
CCompareValidator |
| message |
string |
the user-defined error message. |
CValidator |
| on |
array |
list of scenarios that the validator should be applied. |
CValidator |
| operator |
string |
the operator for comparison. |
CCompareValidator |
| safe |
boolean |
whether attributes listed with this validator should be considered safe for massive assignment. |
CValidator |
| skipOnError |
boolean |
whether this validation rule should be skipped if when there is already a validation
error for the current attribute. |
CValidator |
| strict |
boolean |
whether the comparison is strict (both value and type must be the same. |
CCompareValidator |
Property Details
public boolean $allowEmpty;
whether the attribute value can be null or empty. Defaults to false.
If this is true, it means the attribute is considered valid when it is empty.
public string $compareAttribute;
the name of the attribute to be compared with
public string $compareValue;
the constant value to be compared with
public string $operator;
the operator for comparison. Defaults to '='.
The followings are valid operators:
- '=' or '==': validates to see if the two values are equal. If strict is true, the comparison
will be done in strict mode (i.e. checking value type as well).
- '!=': validates to see if the two values are NOT equal. If strict is true, the comparison
will be done in strict mode (i.e. checking value type as well).
- '>': validates to see if the value being validated is greater than the value being compared with.
- '>=': validates to see if the value being validated is greater than or equal to the value being compared with.
- '<': validates to see if the value being validated is less than the value being compared with.
- '<=': validates to see if the value being validated is less than or equal to the value being compared with.
public boolean $strict;
whether the comparison is strict (both value and type must be the same.)
Defaults to false.
Method Details
protected void validateAttribute( CModel $object, string $attribute)
|
| $object |
CModel |
the object being validated |
| $attribute |
string |
the attribute being validated |
Validates the attribute of the object.
If there is any error, the error message is added to the object.