日本語

Date field

A labeled <input type="date"> bound to a CalendarDate signal, which the browser fills from its own date picker.

Result

text_field.rs
use hypersig::html::div;
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{CalendarDate, Page, Signals, if_else};
use hypersig_ui::{Autocomplete, DateField, InputKind, NumberField, TextField};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Signup {
    email: String,
    bio: String,
    guests: i32,
    arrival: Option<CalendarDate>,
}

pub(super) fn page() -> Page<Signup> {
    let email = Signup::email();
    let looks_wrong = !email.is_empty().or(email.contains("@"));
    let initial = Signup {
        email: String::new(),
        bio: String::new(),
        guests: 1,
        arrival: None,
    };
    Page::new(
        "Sign up",
        initial,
    )
    .child(
        div()
            .css(stack(rem(1.25)).max_width(rem(24)))
            .child(
                TextField::new(
                    "Email",
                    email,
                )
                .kind(InputKind::Email)
                .autocomplete(Autocomplete::Email)
                .placeholder("ada@example.com")
                .description("We send the receipt here.")
                .error(if_else(
                    looks_wrong,
                    "Enter an email address.",
                    "",
                ))
                .required(),
            )
            .child(
                TextField::new(
                    "Bio",
                    Signup::bio(),
                )
                .multiline()
                .description("A sentence or two for your profile."),
            )
            .child(
                NumberField::new(
                    "Guests",
                    Signup::guests(),
                )
                .min(1)
                .max(10)
                .description("Up to 10 people."),
            )
            .child(
                DateField::new(
                    "Arrival",
                    Signup::arrival(),
                )
                .min(CalendarDate::new(2026, 1, 1))
                .max(CalendarDate::new(2026, 12, 31))
                .description("Any day in 2026."),
            ),

    )
}

Accessibility

It is an <input type="date"> with the label, aria-describedby, aria-invalid, and live error of a text field. The browser draws the field and its date picker in the visitor's own date format and language, and gives them their keys, which differ between browsers: in most, Tab or the arrow keys move between the day, month, and year, Up and Down change the part with focus, and a button or Alt+Down opens the calendar. The Arrival field in the demo above is one.

`DateField`

DateField is a builder like TextField. Its signal holds a CalendarDate, a day with no time or time zone, and min and max take a CalendarDate, so a bound cannot be text:

impl<S, T: Dated> DateField<S, T> {
    pub fn new(label: impl Into<String>, value: Sig<S, T>) -> Self
    pub fn min(self, min: CalendarDate) -> Self
    pub fn max(self, max: CalendarDate) -> Self
    pub fn description(self, text: impl Into<String>) -> Self
    pub fn error(self, message: impl IntoExpr<S, String>) -> Self
    pub fn required(self) -> Self
    pub fn disabled(self, when: impl IntoExpr<S, bool>) -> Self
    pub fn render(self) -> El<S>
}

The browser reads and writes the value as YYYY-MM-DD, which is how CalendarDate displays. With min(CalendarDate::new(2026, 1, 1)) and a signal holding April 3, 2026, the field renders as follows, without the ids and the class:

<label for="…">Departure</label>
<input id="…" type="date" min="2026-01-01" value="2026-04-03">

`Dated`

Dated is the bound on T, sealed and implemented for CalendarDate and Option<CalendarDate>. An Option field starts empty for None, and emptying it sets None. A plain CalendarDate always holds a date, and emptying the field keeps the date it had:

pub trait Dated: Bindable + Sealed {}

impl Dated for CalendarDate {}
impl Dated for Option<CalendarDate> {}

A date kept as text, such as a String signal, fails to compile with E0277 and the message a date field cannot be bound to a String signal:

use hypersig::Signals;
use hypersig_ui::DateField;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Booking {
    arrival: String,
}

// A date kept as text, such as "2026-04-01".
let arrival = DateField::new("Arrival", Booking::arrival());

What the types leave unchecked

  • min panics when it is after a maximum set before, and max when it is before a minimum set before. The panic comes where the field is built.
  • The browser's picker offers no day outside min and max, but a day typed outside them still goes to the signal. A plain <form> refuses to submit it, and a form from Sig::form does not, since it has novalidate. Check the range again in an error message or a form rule, and on the server.
  • The field writes a day to the signal only when it is a real date with a year from 1 to 9999. While the visitor has filled only part of it, the signal keeps its last value.

Example: arrival and departure

use hypersig::html::div;
use hypersig::{CalendarDate, Page, Signals};
use hypersig_ui::DateField;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Booking {
    // Empty until the visitor picks a date.
    arrival: Option<CalendarDate>,
    // Always holds a date; emptying the field keeps it.
    departure: CalendarDate,
}

let initial = Booking { arrival: None, departure: CalendarDate::new(2026, 4, 3) };
let page = Page::new("Booking", initial).child(
    div()
        .child(
            DateField::new("Arrival", Booking::arrival())
                .min(CalendarDate::new(2026, 1, 1))
                .max(CalendarDate::new(2026, 12, 31))
                .description("Any day in 2026.")
                .required(),
        )
        .child(DateField::new("Departure", Booking::departure()).min(CalendarDate::new(2026, 1, 2))),
);

Next

Fields bound to a form has DateField::in_form, which takes its message from a form's rules. Number field takes a number in the same frame.