CValidator
| Package |
system.validators |
| Inheritance |
abstract class CValidator »
CComponent |
| Subclasses |
CBooleanValidator, CCaptchaValidator, CCompareValidator, CDefaultValueValidator, CEmailValidator, CExistValidator, CFileValidator, CFilterValidator, CInlineValidator, CNumberValidator, CRangeValidator, CRegularExpressionValidator, CRequiredValidator, CSafeValidator, CStringValidator, CTypeValidator, CUniqueValidator, CUnsafeValidator, CUrlValidator |
| Since |
1.0 |
| Version |
$Id: CValidator.php 2126 2010-05-12 02:15:47Z qiang.xue $ |
CValidator is the base class for all validators.
Child classes must implement the
validateAttribute method.
The following properties are defined in CValidator:
- attributes: array, list of attributes to be validated;
- message: string, the customized error message. The message
may contain placeholders that will be replaced with the actual content.
For example, the "{attribute}" placeholder will be replaced with the label
of the problematic attribute. Different validators may define additional
placeholders.
- on: string, in which scenario should the validator be in effect.
This is used to match the 'on' parameter supplied when calling CModel::validate.
When using
createValidator to create a validator, the following aliases
are recognized as the corresponding built-in validator classes:
Public Properties
Hide inherited properties
| Property | Type | Description | Defined By |
| attributes |
array |
list of attributes to be validated. |
CValidator |
| builtInValidators |
array |
list of built-in validators (name=>class) |
CValidator |
| message |
string |
the user-defined error message. |
CValidator |
| on |
array |
list of scenarios that the validator should be applied. |
CValidator |
| skipOnError |
boolean |
whether this validation rule should be skipped if when there is already a validation
error for the current attribute. |
CValidator |
Property Details
public array $attributes;
list of attributes to be validated.
public static array $builtInValidators;
list of built-in validators (name=>class)
public string $message;
the user-defined error message. Different validators may define various
placeholders in the message that are to be replaced with actual values. All validators
recognize "{attribute}" placeholder, which will be replaced with the label of the attribute.
public array $on;
list of scenarios that the validator should be applied.
Each array value refers to a scenario name with the same name as its array key.
public boolean $skipOnError;
whether this validation rule should be skipped if when there is already a validation
error for the current attribute. Defaults to false.
Method Details
protected void addError( CModel $object, string $attribute, string $message, array $params=array (
))
|
| $object |
CModel |
the data object being validated |
| $attribute |
string |
the attribute being validated |
| $message |
string |
the error message |
| $params |
array |
values for the placeholders in the error message |
Adds an error about the specified attribute to the active record.
This is a helper method that performs message selection and internationalization.
|
public boolean applyTo(string $scenario)
|
| $scenario |
string |
scenario name |
| {return} |
boolean |
whether the validator applies to the specified scenario. |
Returns a value indicating whether the validator applies to the specified scenario.
A validator applies to a scenario as long as any of the following conditions is met:
- the validator's "on" property is empty
- the validator's "on" property contains the specified scenario
public static CValidator createValidator(string $name, CModel $object, mixed $attributes, array $params)
|
| $name |
string |
the name or class of the validator |
| $object |
CModel |
the data object being validated that may contain the inline validation method |
| $attributes |
mixed |
list of attributes to be validated. This can be either an array of
the attribute names or a string of comma-separated attribute names. |
| $params |
array |
initial values to be applied to the validator properties |
| {return} |
CValidator |
the validator |
Creates a validator object.
|
protected boolean isEmpty(mixed $value, boolean $trim=false)
|
| $value |
mixed |
the value to be checked |
| $trim |
boolean |
whether to perform trimming before checking if the string is empty. Defaults to false. |
| {return} |
boolean |
whether the value is empty |
Checks if the given value is empty.
A value is considered empty if it is null, an empty array, or the trimmed result is an empty string.
Note that this method is different from PHP empty(). It will return false when the value is 0.
public void validate( CModel $object, array $attributes=NULL)
|
| $object |
CModel |
the data object being validated |
| $attributes |
array |
the list of attributes to be validated. Defaults to null,
meaning every attribute listed in attributes will be validated. |
Validates the specified object.
abstract protected void validateAttribute( CModel $object, string $attribute)
|
| $object |
CModel |
the data object being validated |
| $attribute |
string |
the name of the attribute to be validated. |
Validates a single attribute.
This method should be overriden by child classes.