22 'date' =>
'[0-9]{4}-[01][0-9]-[0-3][0-9]',
23 'time' =>
'[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?',
24 'datetime' =>
'[0-9]{4}-[01][0-9]-[0-3][0-9][T ][0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?Z?',
34 parent::__construct( $params );
36 $this->mType = $params[
'type'] ??
'datetime';
38 if ( !in_array( $this->mType, [
'date',
'time',
'datetime' ] ) ) {
39 throw new InvalidArgumentException(
"Invalid type '$this->mType'" );
42 if ( $this->mPlaceholder ===
'' ) {
44 $this->mPlaceholder = $this->
msg(
"htmlform-{$this->mType}-placeholder" )->text();
47 $this->mClass .=
' mw-htmlform-datetime-field';
51 $parentList = array_diff( $list, [
'min',
'max' ] );
52 $ret = parent::getAttributes( $parentList );
54 if ( in_array(
'min', $list ) && isset( $this->mParams[
'min'] ) ) {
55 $min = $this->
parseDate( $this->mParams[
'min'] );
60 if ( in_array(
'max', $list ) && isset( $this->mParams[
'max'] ) ) {
61 $max = $this->
parseDate( $this->mParams[
'max'] );
76 if ( !$request->getCheck( $this->mName ) ) {
80 $value = $request->getText( $this->mName );
82 return $date ? $this->
formatDate( $date ) : $value;
86 $p = parent::validate( $value, $alldata );
92 if ( $value ===
'' ) {
100 return $this->
msg(
"htmlform-{$this->mType}-invalid" );
103 if ( isset( $this->mParams[
'min'] ) ) {
104 $min = $this->
parseDate( $this->mParams[
'min'] );
105 if ( $min && $date < $min ) {
107 return $this->
msg(
"htmlform-{$this->mType}-toolow", $this->
formatDate( $min ) );
111 if ( isset( $this->mParams[
'max'] ) ) {
112 $max = $this->
parseDate( $this->mParams[
'max'] );
113 if ( $max && $date > $max ) {
115 return $this->
msg(
"htmlform-{$this->mType}-toohigh", $this->
formatDate( $max ) );
123 $value = trim( $value ??
'' );
124 if ( $value ===
'' ) {
128 if ( $this->mType ===
'date' ) {
129 $value .=
' T00:00:00+0000';
131 if ( $this->mType ===
'time' ) {
132 $value =
'1970-01-01 ' . $value .
'+0000';
136 $date =
new DateTime( $value,
new DateTimeZone(
'GMT' ) );
137 return $date->getTimestamp();
138 }
catch ( Exception $ex ) {
144 switch ( $this->mType ) {
146 return gmdate(
'Y-m-d', $value );
149 return gmdate(
'H:i:s', $value );
152 return gmdate(
'Y-m-d\\TH:i:s\\Z', $value );
164 $params += OOUI\Element::configFromHtmlAttributes(
165 $this->
getAttributes( [
'disabled',
'readonly',
'min',
'max' ] )
168 if ( $this->mType ===
'date' ) {
169 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.DateInputWidget.styles' );
177 if ( $this->mType ===
'date' ) {
178 return [
'mediawiki.widgets.DateInputWidget' ];
180 return [
'mediawiki.widgets.datetime' ];
A field that will contain a date and/or time.
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
shouldInfuseOOUI()
Whether the field should be automatically infused.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getAttributes(array $list)
Returns the given attributes from the parameters.