Date Input

Renders <input type="date"> with a browser-native date picker. Values are formatted as YYYY-MM-DD. Uses a custom date sanitizer by default.

Class: PinkCrab\Form_Components\Element\Field\Input\Date
Make helper: Make::date( 'name', fn(Date $f) => $f->... )


Basic Usage

$this->component( new Input_Component(
		Date::make( 'birthday' )
			->label( 'Date of Birth' )
	) )

Basic

Generated HTML
<div id="form-field_birthday" class="pc-form__element pc-form__element--date_input">
    <label for="birthday" class="pc-form__label">Date of Birth</label>
        <input type="date" name="birthday" class="form-control date-input pc-form__element__field pc-form__element__field--date_input" list="_birthday__list" />
    </div>

Using Make Helper

use PinkCrab\Form_Components\Util\Make;

$this->component( Make::date( 'birthday', fn( $f ) => $f
    ->label( 'Date of Birth' )
    ->required( true )
    ->min( '1900-01-01' )
    ->max( '2026-12-31' )
) );

Methods

label( string $label )

Sets the visible label text above the input.

Date::make( 'birthday' )->label( 'Date of Birth' )
Generated HTML
<div id="form-field_birthday" class="pc-form__element pc-form__element--date_input">
    <label for="birthday" class="pc-form__label">Date of Birth</label>
    <input type="date" name="birthday"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
    />
</div>

set_existing( mixed $value )

Sets the current value. Runs through a date format sanitizer (Y-m-d) by default.

Date::make( 'event_date' )
			->label( 'Event Date' )
			->set_existing( '2026-06-15' )

set_existing

Generated HTML
<div id="form-field_event_date" class="pc-form__element pc-form__element--date_input">
    <label for="event_date" class="pc-form__label">Event Date</label>
        <input type="date" name="event_date" class="form-control date-input pc-form__element__field pc-form__element__field--date_input" list="_event_date__list" value="2026-06-15" />
    </div>

min( int|float|string|null $min )

Sets the earliest allowed date.

Date::make( 'booking' )
    ->label( 'Booking Date' )
    ->min( '2026-01-01' )
Generated HTML
<div id="form-field_booking" class="pc-form__element pc-form__element--date_input">
    <label for="booking" class="pc-form__label">Booking Date</label>
    <input type="date" name="booking"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        min="2026-01-01"
    />
</div>

max( int|float|string|null $max )

Sets the latest allowed date.

Date::make( 'booking' )
    ->label( 'Booking Date' )
    ->max( '2026-12-31' )
Generated HTML
<div id="form-field_booking" class="pc-form__element pc-form__element--date_input">
    <label for="booking" class="pc-form__label">Booking Date</label>
    <input type="date" name="booking"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        max="2026-12-31"
    />
</div>

step_by_days( int $days )

Sets the step increment in days. Wrapper around step().

Date::make( 'weekly' )
    ->label( 'Select Date' )
    ->step_by_days( 1 )
Generated HTML
<div id="form-field_weekly" class="pc-form__element pc-form__element--date_input">
    <label for="weekly" class="pc-form__label">Select Date</label>
    <input type="date" name="weekly"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        step="1"
    />
</div>

step_by_weeks( int $weeks )

Sets the step increment in weeks (multiplied by 7).

Date::make( 'biweekly' )
    ->label( 'Select Fortnight' )
    ->step_by_weeks( 2 )
Generated HTML
<div id="form-field_biweekly" class="pc-form__element pc-form__element--date_input">
    <label for="biweekly" class="pc-form__label">Select Fortnight</label>
    <input type="date" name="biweekly"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        step="14"
    />
</div>

required( bool $required = true )

Marks the field as required. The label displays a * indicator via CSS.

Date::make( 'start_date' )
    ->label( 'Start Date' )
    ->required( true )
Generated HTML
<div id="form-field_start_date" class="pc-form__element pc-form__element--date_input">
    <label for="start_date" class="pc-form__label">Start Date</label>
    <input type="date" name="start_date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        required=""
    />
</div>

disabled( bool $disabled = true )

Disables the input. Value is visible but cannot be changed or submitted.

Date::make( 'locked_date' )
    ->label( 'Locked' )
    ->set_existing( '2026-01-01' )
    ->disabled( true )
Generated HTML
<div id="form-field_locked_date" class="pc-form__element pc-form__element--date_input">
    <label for="locked_date" class="pc-form__label">Locked</label>
    <input type="date" name="locked_date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        disabled="" value="2026-01-01"
    />
</div>

readonly( bool $readonly = true )

Makes the field read-only.

Date::make( 'confirmed' )
    ->label( 'Confirmed Date' )
    ->set_existing( '2026-06-15' )
    ->readonly( true )
Generated HTML
<div id="form-field_confirmed" class="pc-form__element pc-form__element--date_input">
    <label for="confirmed" class="pc-form__label">Confirmed Date</label>
    <input type="date" name="confirmed"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        readonly="" value="2026-06-15"
    />
</div>

autocomplete( string $value )

HTML autocomplete attribute to help browsers autofill.

Date::make( 'bday' )
    ->label( 'Birthday' )
    ->autocomplete( 'bday' )
Generated HTML
<div id="form-field_bday" class="pc-form__element pc-form__element--date_input">
    <label for="bday" class="pc-form__label">Birthday</label>
    <input type="date" name="bday"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        autocomplete="bday"
    />
</div>

Common values:

Value Description
off Disable autocomplete
on Enable autocomplete (browser decides)
name Full name
given-name First name
family-name Last name
email Email address
username Username
new-password New password (password managers)
current-password Current password
organization Company/organisation name
street-address Street address
address-line1 Address line 1
address-line2 Address line 2
address-level2 City
address-level1 State/province/region
country Country code
country-name Country name
postal-code Postcode / ZIP
tel Full phone number
tel-national Phone without country code
url URL
bday Full date of birth
bday-day Day of birth
bday-month Month of birth
bday-year Year of birth
sex Gender
cc-name Cardholder name
cc-number Card number
cc-exp Card expiry
cc-csc Card security code

datalist_items( array $items )

Suggested date values via an HTML <datalist> element.

Date::make( 'holiday' )
    ->label( 'Holiday' )
    ->datalist_items( array( '2026-01-01', '2026-12-25', '2026-12-26' ) )
Generated HTML
<div id="form-field_holiday" class="pc-form__element pc-form__element--date_input">
    <label for="holiday" class="pc-form__label">Holiday</label>
    <input type="date" name="holiday"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        list="_holiday__list"
    />
    <datalist id="_holiday__list">
        <option value="2026-01-01"></option>
        <option value="2026-12-25"></option>
        <option value="2026-12-26"></option>
    </datalist>
</div>

error_notification( string $message )

Displays an error message below the field.

Date::make( 'invalid_date' )
    ->label( 'Date' )
    ->error_notification( 'Please select a valid date.' )
Generated HTML
<div id="form-field_invalid_date" class="pc-form__element pc-form__element--date_input notification-error">
    <label for="invalid_date" class="pc-form__label">Date</label>
    <input type="date" name="invalid_date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input notification-error"
    />
    <div class="pc-form__notification pc-form__notification--error">Please select a valid date.</div>
</div>

warning_notification( string $message )

Displays a warning message below the field.

Date::make( 'past_date' )
    ->label( 'Date' )
    ->set_existing( '2020-01-01' )
    ->warning_notification( 'This date is in the past.' )
Generated HTML
<div id="form-field_past_date" class="pc-form__element pc-form__element--date_input notification-warning">
    <label for="past_date" class="pc-form__label">Date</label>
    <input type="date" name="past_date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input notification-warning"
        value="2020-01-01"
    />
    <div class="pc-form__notification pc-form__notification--warning">This date is in the past.</div>
</div>

success_notification( string $message )

Displays a success message below the field.

Date::make( 'ok_date' )
    ->label( 'Date' )
    ->set_existing( '2026-06-15' )
    ->success_notification( 'Date is available.' )
Generated HTML
<div id="form-field_ok_date" class="pc-form__element pc-form__element--date_input notification-success">
    <label for="ok_date" class="pc-form__label">Date</label>
    <input type="date" name="ok_date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input notification-success"
        value="2026-06-15"
    />
    <div class="pc-form__notification pc-form__notification--success">Date is available.</div>
</div>

info_notification( string $message )

Displays an info message below the field.

Date::make( 'info_date' )
    ->label( 'Date' )
    ->info_notification( 'Select a date within the next 30 days.' )
Generated HTML
<div id="form-field_info_date" class="pc-form__element pc-form__element--date_input notification-info">
    <label for="info_date" class="pc-form__label">Date</label>
    <input type="date" name="info_date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input notification-info"
    />
    <div class="pc-form__notification pc-form__notification--info">Select a date within the next 30 days.</div>
</div>

pre_description( string $description )

Sets a description or hint displayed before the input.

Date::make( 'birthday' )
    ->label( 'Date of Birth' )
    ->pre_description( 'Select your date of birth.' )

post_description( string $description )

Sets a description or help text displayed after the input, before any notification.

Date::make( 'birthday' )
    ->label( 'Date of Birth' )
    ->post_description( 'Format: YYYY-MM-DD' )

before( string $html ) / after( string $html )

HTML content before or after the input within the wrapper.

Date::make( 'wrapped_date' )
			->label( 'Event Date' )
			->before( '<span style="color:#6b7280;font-size:13px;">Select your preferred date</span>' )
			->after( '<span style="color:#6b7280;font-size:13px;">Format: YYYY-MM-DD</span>' )

before/after

Generated HTML
<div id="form-field_wrapped_date" class="pc-form__element pc-form__element--date_input">
    <span style="color:#6b7280;font-size:13px">Select your preferred date</span>
        <label for="wrapped_date" class="pc-form__label">Event Date</label>
            <input type="date" name="wrapped_date" class="form-control date-input pc-form__element__field pc-form__element__field--date_input" list="_wrapped_date__list" />
            <span style="color:#6b7280;font-size:13px">Format: YYYY-MM-DD</span>
            </div>

id( string $id )

Sets a custom HTML id on the input element.

Date::make( 'date' )->id( 'my-date-picker' )
Generated HTML
<div id="form-field_date" class="pc-form__element pc-form__element--date_input">
    <input type="date" name="date" id="my-date-picker"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
    />
</div>

wrapper_id( string $id )

Sets a custom HTML id on the wrapper div.

Date::make( 'date' )->wrapper_id( 'date-wrapper' )
Generated HTML
<div id="date-wrapper" class="pc-form__element pc-form__element--date_input">
    <input type="date" name="date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
    />
</div>

data( string $key, string $value )

Adds a data-* attribute to the input.

Date::make( 'date' )->data( 'format', 'iso' )
Generated HTML
<div id="form-field_date" class="pc-form__element pc-form__element--date_input">
    <input type="date" name="date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        data-format="iso"
    />
</div>

wrapper_data( string $key, string $value )

Adds a data-* attribute to the wrapper div.

Date::make( 'date' )->wrapper_data( 'section', 'booking' )
Generated HTML
<div id="form-field_date" class="pc-form__element pc-form__element--date_input" data-section="booking">
    <input type="date" name="date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
    />
</div>

add_class( string $class )

Adds a CSS class to the input element.

Date::make( 'date' )->add_class( 'wide-picker' )
Generated HTML
<div id="form-field_date" class="pc-form__element pc-form__element--date_input">
    <input type="date" name="date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input wide-picker"
    />
</div>

add_wrapper_class( string $class )

Adds a CSS class to the wrapper div.

Date::make( 'date' )->add_wrapper_class( 'date-field' )
Generated HTML
<div id="form-field_date" class="pc-form__element pc-form__element--date_input date-field">
    <input type="date" name="date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
    />
</div>

show_wrapper( bool $show = true )

Controls whether the wrapping <div> is rendered.

Date::make( 'date' )->show_wrapper( false )
Generated HTML
<input type="date" name="date"
    class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
/>

tabindex( int $index )

Sets the tab order of the input.

Date::make( 'date' )->tabindex( 4 )
Generated HTML
<div id="form-field_date" class="pc-form__element pc-form__element--date_input">
    <input type="date" name="date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        tabindex="4"
    />
</div>

attribute( string $key, mixed $value )

Sets an arbitrary HTML attribute on the input.

Date::make( 'date' )->attribute( 'aria-label', 'Select a date' )
Generated HTML
<div id="form-field_date" class="pc-form__element pc-form__element--date_input">
    <input type="date" name="date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        aria-label="Select a date"
    />
</div>

attributes( array $attrs )

Sets multiple arbitrary HTML attributes at once.

Date::make( 'date' )->attributes( array(
    'title' => 'Pick a date',
    'tabindex' => '4',
) )
Generated HTML
<div id="form-field_date" class="pc-form__element pc-form__element--date_input">
    <input type="date" name="date"
        class="form-control date-input pc-form__element__field pc-form__element__field--date_input"
        title="Pick a date" tabindex="4"
    />
</div>

sanitizer( callable $fn )

Sets a sanitization callback applied when set_existing() is called. Default: custom date sanitizer that validates Y-m-d format using DateTimeImmutable.

Using the default (automatic):

Date::make( 'date' )
    ->set_existing( '2026-06-15' ) // Validates and stores as Y-m-d

Using a custom callable:

Date::make( 'date' )
    ->sanitizer( function( $value ) {
        $date = DateTimeImmutable::createFromFormat( 'Y-m-d', $value );
        return $date ? $date->format( 'Y-m-d' ) : '';
    } )
    ->set_existing( '2026-06-15' )

Built-in sanitizer helpers:

Constant Function Description
Sanitize::TEXT sanitize_text_field() Strips tags, removes extra whitespace
Sanitize::TEXTAREA sanitize_textarea_field() Like TEXT but preserves line breaks
Sanitize::URL esc_url_raw() Sanitises a URL for database storage
Sanitize::EMAIL sanitize_email() Strips invalid email characters
Sanitize::HEX_COLOR sanitize_hex_color() Validates hex colour (#fff or #ffffff)
Sanitize::NUMBER Custom numeric parser Parses to int or float
Sanitize::NOOP Pass-through No sanitization applied

validator( Validator $validator )

Sets a Respect\Validation validator for server-side validation.

use Respect\Validation\Validator as v;

Date::make( 'date' )->validator( v::date( 'Y-m-d' ) )

style( Style $style )

Sets a custom style for the field, overriding the default.

use PinkCrab\Form_Components\Style\Default_Style;

Date::make( 'date' )->style( new Default_Style() )

Traits

Trait Methods
Label label(), get_label(), has_label()
Single_Value value(), get_value(), has_value()
Range min(), max(), get_min(), get_max()
Required required(), is_required()
Disabled disabled(), is_disabled()
Read_Only readonly(), is_read_only()
Autocomplete autocomplete(), get_autocomplete(), has_autocomplete()
Datalist datalist_items(), get_datalist_key(), get_datalist_items()
Description pre_description(), post_description(), get_pre_description(), get_post_description(), has_pre_description(), has_post_description()
Notification error_notification(), warning_notification(), success_notification(), info_notification()

This site uses Just the Docs, a documentation theme for Jekyll.