CValidator
| Package | system.validators |
|---|---|
| Inheritance | abstract class CValidator » CComponent |
| Subclasses | CCaptchaValidator, CCompareValidator, CDefaultValueValidator, CEmailValidator, CFileValidator, CFilterValidator, CInlineValidator, CNumberValidator, CRangeValidator, CRegularExpressionValidator, CRequiredValidator, CStringValidator, CTypeValidator, CUniqueValidator, CUrlValidator |
| Since | 1.0 |
| Version | $Id: CValidator.php 545 2009-01-19 13:13:50Z qiang.xue $ |
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:
- required: CRequiredValidator
- filter: CFilterValidator
- match: CRegularExpressionValidator
- email: CEmailValidator
- url: CUrlValidator
- unique: CUniqueValidator
- compare: CCompareValidator
- length: CStringValidator
- in: CRangeValidator
- numerical: CNumberValidator
- captcha: CCaptchaValidator
- type: CTypeValidator
- file: CFileValidator
- default: CDefaultValueValidator
Public 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 |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __call() | Calls the named method which is not a class method. | CComponent |
| __get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
| __isset() | Checks if a property value is null. | CComponent |
| __set() | Sets value of a component property. | CComponent |
| __unset() | Sets a component property to be null. | CComponent |
| applyTo() | Returns a value indicating whether the validator applies to the specified scenario. | CValidator |
| asa() | Returns the named behavior object. | CComponent |
| attachBehavior() | Attaches a behavior to this component. | CComponent |
| attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
| attachEventHandler() | Attaches an event handler to an event. | CComponent |
| canGetProperty() | Determines whether a property can be read. | CComponent |
| canSetProperty() | Determines whether a property can be set. | CComponent |
| createValidator() | Creates a validator object. | CValidator |
| detachBehavior() | Detaches a behavior from the component. | CComponent |
| detachBehaviors() | Detaches all behaviors from the component. | CComponent |
| detachEventHandler() | Detaches an existing event handler. | CComponent |
| disableBehavior() | Disables an attached behavior. | CComponent |
| disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
| enableBehavior() | Enables an attached behavior. | CComponent |
| enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
| getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
| hasEvent() | Determines whether an event is defined. | CComponent |
| hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
| hasProperty() | Determines whether a property is defined. | CComponent |
| raiseEvent() | Raises an event. | CComponent |
| validate() | Validates the specified object. | CValidator |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| addError() | Adds an error about the specified attribute to the active record. | CValidator |
| validateAttribute() | Validates a single attribute. | CValidator |
Property Details
list of attributes to be validated.
list of built-in validators (name=>class)
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.
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.
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 void 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 |
Creates a validator object.
|
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.