Class yii\validators\DateValidator
Inheritance | yii\validators\DateValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
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. For values that include time information (not date-only values)
also the time zone will be adjusted. The time zone of the input value is assumed to be the one specified by the $timeZone
property and the target timeZone will be UTC when $timestampAttributeFormat is null
(exporting as UNIX timestamp)
or $timestampAttributeTimeZone otherwise. If you want to avoid the time zone conversion, make sure that $timeZone and
$timestampAttributeTimeZone are the same.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$attributeNames | array | Attribute names. | yii\validators\Validator |
$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 |
$type | string | The type of the validator. | yii\validators\DateValidator |
$validationAttributes | array | List of attribute names. | yii\validators\Validator |
$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\BaseObject |
__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\BaseObject |
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 |
getAttributeNames() | Returns cleaned attribute names without the ! character at the beginning. |
yii\validators\Validator |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
getClientOptions() | Returns the client-side validation options. | yii\validators\Validator |
getValidationAttributes() | Returns a list of attributes this validator applies to. | yii\validators\Validator |
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 |
---|---|---|
formatMessage() | Formats a mesage using the I18N, or simple strtr if \Yii::$app is not available. |
yii\validators\Validator |
parseDateValue() | Parses date string into UNIX timestamp. | yii\validators\DateValidator |
validateValue() | Validates a value. | yii\validators\DateValidator |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
TYPE_DATE | 'date' | Constant for specifying the validation $type as a date value, used for validation with intl short format. | yii\validators\DateValidator |
TYPE_DATETIME | 'datetime' | Constant for specifying the validation $type as a datetime value, used for validation with intl short format. | yii\validators\DateValidator |
TYPE_TIME | 'time' | Constant for specifying the validation $type as a time value, used for validation with intl short format. | 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 https://secure.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.
Since version 2.0.8 the default value will be determined from different formats of the formatter class,
dependent on the value of $type:
- if type is TYPE_DATE, the default value will be taken from yii\i18n\Formatter::$dateFormat,
- if type is TYPE_DATETIME, it will be taken from yii\i18n\Formatter::$datetimeFormat,
- and if type is TYPE_TIME, it will be yii\i18n\Formatter::$timeFormat.
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.
Note, that when using this property, the input value will be converted to a unix timestamp,
which by definition is in UTC, so a conversion from the input time zone to UTC
will be performed. When defining $timestampAttributeFormat you can control the conversion by
setting $timestampAttributeTimeZone to a different value than 'UTC'
.
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.
The type of the validator. Indicates, whether a date, time or datetime value should be validated.
This property influences the default value of $format and also sets the correct behavior when $format is one of the intl
short formats, short
, medium
, long
, or full
.
This is only effective when the PHP intl extension is installed.
This property can be set to the following values:
- TYPE_DATE - (default) for validating date values only, that means only values that do not include a time range are valid.
- TYPE_DATETIME - for validating datetime values, that contain a date part as well as a time part.
- TYPE_TIME - for validating time values, that contain no date information.
Method Details
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public void init ( ) |
Parses date string into UNIX timestamp.
protected 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.
public 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.
protected array|null validateValue ( $value ) | ||
$value | mixed | The data value to be validated. |
return | array|null | The error message and the array of parameters to be inserted into the error message.
} return null;
Null should be returned if the data is valid. |
---|---|---|
throws | yii\base\NotSupportedException | if the validator does not supporting data validation without a model |
Signup or Login in order to comment.