Class yii\mongodb\validators\MongoDateValidator
| Inheritance | yii\ |
|---|---|
| Available since extension's version | 2.0.4 |
| Source Code | https://github.com/yiisoft/yii2-mongodb/blob/master/src/validators/MongoDateValidator.php |
MongoDateValidator is an enhanced version of DateValidator, which supports \
Usage example:
class Customer extends yii\mongodb\ActiveRecord
{
...
public function rules()
{
return [
['date', 'yii\mongodb\validators\MongoDateValidator' , 'format' => 'MM/dd/yyyy']
];
}
}
See also \
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $mongoDateAttribute | string | The name of the attribute to receive the parsing result as \ |
yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validateAttribute() | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| parseDateValue() | yii\ |
Property Details
The name of the attribute to receive the parsing result as \
This can be the same attribute as the one being validated. If this is the case, the original value will be overwritten with the value after successful validation.
Method Details
| protected parseDateValue ( mixed $value ) | ||
| $value | mixed | |
protected function parseDateValue($value)
{
return $value instanceof UTCDateTime
? $value->toDateTime()->getTimestamp()
: parent::parseDateValue($value);
}
| public validateAttribute ( mixed $model, mixed $attribute ) | ||
| $model | mixed | |
| $attribute | mixed | |
public function validateAttribute($model, $attribute)
{
$mongoDateAttribute = $this->mongoDateAttribute;
if ($this->timestampAttribute === null) {
$this->timestampAttribute = $mongoDateAttribute;
}
$originalErrorCount = count($model->getErrors($attribute));
parent::validateAttribute($model, $attribute);
$afterValidateErrorCount = count($model->getErrors($attribute));
if ($originalErrorCount === $afterValidateErrorCount) {
if ($this->mongoDateAttribute !== null) {
$timestamp = $model->{$this->timestampAttribute};
$mongoDateAttributeValue = $model->{$this->mongoDateAttribute};
// ensure "dirty attributes" support :
if (!($mongoDateAttributeValue instanceof UTCDateTime) || $mongoDateAttributeValue->sec !== $timestamp) {
$model->{$this->mongoDateAttribute} = new UTCDateTime($timestamp * 1000);
}
}
}
}