CDateValidator
包 | system.validators |
---|---|
继承 | class CDateValidator » CValidator » CComponent |
可用自 | 1.1.7 |
版本 | $Id: CDateValidator.php 2799 2011-01-01 19:31:13Z qiang.xue $ |
源码 | framework/validators/CDateValidator.php |
CDateValidator verifies if the attribute represents a date, time or datetime.
By setting the format property, one can specify what format the date value must be in. If the given date value doesn't follow the format, the attribute is considered as invalid.
By setting the format property, one can specify what format the date value must be in. If the given date value doesn't follow the format, the attribute is considered as invalid.
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
allowEmpty | boolean | whether the attribute value can be null or empty. | CDateValidator |
attributes | array | list of attributes to be validated. | CValidator |
builtInValidators | array | list of built-in validators (name=>class) | CValidator |
enableClientValidation | boolean | whether to perform client-side validation. | CValidator |
except | array | list of scenarios that the validator should not be applied to. | CValidator |
format | mixed | the format pattern that the date value should follow. | CDateValidator |
message | string | the user-defined error message. | CValidator |
on | array | list of scenarios that the validator should be applied. | CValidator |
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 when there is already a validation error for the current attribute. | CValidator |
timestampAttribute | string | the name of the attribute to receive the parsing result. | CDateValidator |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__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 |
clientValidateAttribute() | Returns the JavaScript needed for performing client-side validation. | CValidator |
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 |
evaluateExpression() | Evaluates a PHP expression or callback under the context of 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 |
受保护的方法
方法 | 描述 | 被定义在 |
---|---|---|
addError() | Adds an error about the specified attribute to the active record. | CValidator |
isEmpty() | Checks if the given value is empty. | CValidator |
validateAttribute() | Validates the attribute of the object. | CDateValidator |
属性详情
allowEmpty
属性
public boolean $allowEmpty;
whether the attribute value can be null or empty. Defaults to true, meaning that if the attribute is empty, it is considered valid.
format
属性
public mixed $format;
the format pattern that the date value should follow. This can be either a string or an array representing multiple formats. Defaults to 'MM/dd/yyyy'. Please see CDateTimeParser for details about how to specify a date format.
timestampAttribute
属性
public string $timestampAttribute;
the name of the attribute to receive the parsing result. When this property is not null and the validation is successful, the named attribute will receive the parsing result.
方法详情
validateAttribute()
方法
protected void validateAttribute(CModel $object, string $attribute)
| ||
$object | CModel | the object being validated |
$attribute | string | the attribute being validated |
源码: framework/validators/CDateValidator.php#49 (显示)
protected function validateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty && $this->isEmpty($value))
return;
$valid=false;
// reason of array checking is explained here: https://github.com/yiisoft/yii/issues/1955
if(!is_array($value))
{
$formats=is_string($this->format) ? array($this->format) : $this->format;
foreach($formats as $format)
{
$timestamp=CDateTimeParser::parse($value,$format,array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0));
if($timestamp!==false)
{
$valid=true;
if($this->timestampAttribute!==null)
$object->{$this->timestampAttribute}=$timestamp;
break;
}
}
}
if(!$valid)
{
$message=$this->message!==null?$this->message : Yii::t('yii','The format of {attribute} is invalid.');
$this->addError($object,$attribute,$message);
}
}
Validates the attribute of the object. If there is any error, the error message is added to the object.