Class yii\validators\DateValidator
Inheritance | yii\validators\DateValidator » yii\validators\Validator » yii\base\Component » yii\base\Object |
---|---|
Implements | yii\base\Configurable |
Subclasses | yii\mongodb\validators\MongoDateValidator |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/validators/DateValidator.php |
DateValidator verifies if the attribute represents a date, time or datetime in a proper $format.
It can also parse internationalized dates in a specific $locale like e.g. 12 мая 2014
when $format
is configured to use a time pattern in ICU format.
It is further possible to limit the date within a certain range using $min and $max.
Additional to validating the date it can also export the parsed timestamp as a machine readable format which can be configured using $timestampAttribute.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$attributes | array|string | Attributes to be validated by this validator. | yii\validators\Validator |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$builtInValidators | array | List of built-in validators (name => class or configuration) | yii\validators\Validator |
$enableClientValidation | boolean | Whether to enable client-side validation for this validator. | yii\validators\Validator |
$except | array|string | Scenarios that the validator should not be applied to. | yii\validators\Validator |
$format | string | The date format that the value being validated should follow. | yii\validators\DateValidator |
$isEmpty | callable | A PHP callable that replaces the default implementation of isEmpty(). | yii\validators\Validator |
$locale | string | The locale ID that is used to localize the date parsing. | yii\validators\DateValidator |
$max | integer|string | Upper limit of the date. | yii\validators\DateValidator |
$maxString | string | User friendly value of upper limit to display in the error message. | yii\validators\DateValidator |
$message | string | The user-defined error message. | yii\validators\Validator |
$min | integer|string | Lower limit of the date. | yii\validators\DateValidator |
$minString | string | User friendly value of lower limit to display in the error message. | yii\validators\DateValidator |
$on | array|string | Scenarios that the validator can be applied to. | yii\validators\Validator |
$skipOnEmpty | boolean | Whether this validation rule should be skipped if the attribute value is null or an empty string. | yii\validators\Validator |
$skipOnError | boolean | Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. | yii\validators\Validator |
$timeZone | string | The timezone to use for parsing date and time values. | yii\validators\DateValidator |
$timestampAttribute | string | The name of the attribute to receive the parsing result. | yii\validators\DateValidator |
$timestampAttributeFormat | string | The format to use when populating the $timestampAttribute. | yii\validators\DateValidator |
$timestampAttributeTimeZone | string | The timezone to use when populating the $timestampAttribute. | yii\validators\DateValidator |
$tooBig | string | User-defined error message used when the value is bigger than $max. | yii\validators\DateValidator |
$tooSmall | string | User-defined error message used when the value is smaller than $min. | yii\validators\DateValidator |
$when | callable | A PHP callable whose return value determines whether this validator should be applied. | yii\validators\Validator |
$whenClient | string | A JavaScript function name whose return value determines whether this validator should be applied on the client side. | yii\validators\Validator |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\Object |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
addError() | Adds an error about the specified attribute to the model object. | yii\validators\Validator |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\Object |
clientValidateAttribute() | Returns the JavaScript needed for performing client-side validation. | yii\validators\Validator |
createValidator() | Creates a validator object. | yii\validators\Validator |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
init() | Initializes the object. | yii\validators\DateValidator |
isActive() | Returns a value indicating whether the validator is active for the given scenario and attribute. | yii\validators\Validator |
isEmpty() | Checks if the given value is empty. | yii\validators\Validator |
off() | Detaches an existing event handler from this component. | yii\base\Component |
on() | Attaches an event handler to an event. | yii\base\Component |
trigger() | Triggers an event. | yii\base\Component |
validate() | Validates a given value. | yii\validators\Validator |
validateAttribute() | Validates a single attribute. | yii\validators\DateValidator |
validateAttributes() | Validates the specified object. | yii\validators\Validator |
Protected Methods
Method | Description | Defined By |
---|---|---|
parseDateValue() | Parses date string into UNIX timestamp | yii\validators\DateValidator |
validateValue() | Validates a value. | yii\validators\DateValidator |
Property Details
The date format that the value being validated should follow. This can be a date time pattern as described in the ICU manual.
Alternatively this can be a string prefixed with php:
representing a format that can be recognized by the PHP Datetime class.
Please refer to http://php.net/manual/en/datetime.createfromformat.php on supported formats.
If this property is not set, the default value will be obtained from Yii::$app->formatter->dateFormat
, see yii\i18n\Formatter::$dateFormat for details.
Here are some example values:
'MM/dd/yyyy' // date in ICU format
'php:m/d/Y' // the same date in PHP format
'MM/dd/yyyy HH:mm' // not only dates but also times can be validated
Note: the underlying date parsers being used vary dependent on the format. If you use the ICU format and
the PHP intl extension is installed, the IntlDateFormatter
is used to parse the input value. In all other cases the PHP DateTime class
is used. The IntlDateFormatter has the advantage that it can parse international dates like 12. Mai 2015
or 12 мая 2014
, while the
PHP parser is limited to English only. The PHP parser however is more strict about the input format as it will not accept
12.05.05
for the format php:d.m.Y
, but the IntlDateFormatter will accept it for the format dd-MM-yyyy
.
If you need to use the IntlDateFormatter you can avoid this problem by specifying a minimum date.
The locale ID that is used to localize the date parsing. This is only effective when the PHP intl extension is installed. If not set, the locale of the formatter will be used. See also yii\i18n\Formatter::$locale.
Upper limit of the date. Defaults to null, meaning no upper limit. This can be a unix timestamp or a string representing a date time value. If this property is a string, $format will be used to parse it.
See also $tooBig for the customized message used when the date is too big.
User friendly value of upper limit to display in the error message. If this property is null, the value of $max will be used (before parsing).
Lower limit of the date. Defaults to null, meaning no lower limit. This can be a unix timestamp or a string representing a date time value. If this property is a string, $format will be used to parse it.
See also $tooSmall for the customized message used when the date is too small.
User friendly value of lower limit to display in the error message. If this property is null, the value of $min will be used (before parsing).
The timezone to use for parsing date and time values.
This can be any value that may be passed to date_default_timezone_set()
e.g. UTC
, Europe/Berlin
or America/Chicago
.
Refer to the php manual for available timezones.
If this property is not set, yii\base\Application::$timeZone will be used.
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.
This can be the same attribute as the one being validated. If this is the case, the original value will be overwritten with the timestamp value after successful validation.
See also:
The format to use when populating the $timestampAttribute. The format can be specified in the same way as for $format.
If not set, $timestampAttribute will receive a UNIX timestamp. If $timestampAttribute is not set, this property will be ignored.
See also:
The timezone to use when populating the $timestampAttribute. Defaults to UTC
.
This can be any value that may be passed to date_default_timezone_set()
e.g. UTC
, Europe/Berlin
or America/Chicago
.
Refer to the php manual for available timezones.
If $timestampAttributeFormat is not set, this property will be ignored.
See also $timestampAttributeFormat.
User-defined error message used when the value is bigger than $max.
User-defined error message used when the value is smaller than $min.
Method Details
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
void init( ) |
Parses date string into UNIX timestamp
integer|false parseDateValue( $value ) | ||
$value | string | String representing date |
return | integer|false | A UNIX timestamp or |
---|
Validates a single attribute.
Child classes must implement this method to provide the actual validation logic.
void validateAttribute( $model, $attribute ) | ||
$model | yii\base\Model | The data model to be validated |
$attribute | string | The name of the attribute to be validated. |
Validates a value.
A validator class can implement this method to support data validation out of the context of a data model.
array|null validateValue( $value ) | ||
$value | mixed | The data value to be validated. |
return | array|null | The error message and the parameters to be inserted into the error message. Null should be returned if the data is valid. |
---|---|---|
throws | yii\base\NotSupportedException | if the validator does not supporting data validation without a model |