Skip to content

Field

A labelled field for the control declared by its children. The label names the control for assistive technology and focuses it on click. It sits above the control, or beside it in a form.

ui.field

Basic

ui.field("Full name").children(|ui| {
ui.text_input(&mut self.full_name).placeholder("Jane Doe");
});

Required, with an error

// The error shows until the value is one you accept.
let invalid = !self.email.contains('@');
let field = ui.field("Email").required();
let field = field.when(invalid, |f| f.error("Enter an email address."));
field.children(|ui| {
ui.text_input(&mut self.email).placeholder("name@example.com");
});

With a description

ui.field("Username").description("Letters, digits and dashes.").children(|ui| {
ui.text_input(&mut self.username);
});

Any control

let roles = [(0, "Viewer"), (1, "Editor"), (2, "Owner")];
ui.column().w_fill().gap(16).children(|ui| {
ui.field("Role").children(|ui| {
ui.select(&mut self.role, &roles).w_fill();
});
ui.field("Seats").description("From 1 to 99.").children(|ui| {
ui.number_input(&mut self.seats, 1..=99).w(120);
});
ui.field("About").children(|ui| {
ui.text_input(&mut self.about).placeholder("A few words").multiline(3);
});
ui.field("Updates").children(|ui| {
ui.switch(&mut self.updates, "Email me about new versions");
});
});

Several fields and a submit button

ui.column().w_fill().gap(16).children(|ui| {
ui.field("Project").required().children(|ui| {
ui.text_input(&mut self.project_name).placeholder("my-app");
});
ui.field("Description")
.description("Shown on the project page.")
.children(|ui| {
ui.text_input(&mut self.project_about);
});
ui.row().w_fill().gap(8).justify_end().children(|ui| {
ui.button("Cancel").outline();
// Nothing to send without the required field.
ui.button("Create").primary().disabled(self.project_name.is_empty());
});
});

API reference

SettingDescription
.description(text: impl Display)A note below the control.
.error(text: impl Display)An error in the danger color below the control, whose value is reported as invalid. Text inputs, selects, comboboxes and pickers in the field also take the danger color in their border and focus ring, unless error_frame turns that off. Pass it only while there is one, as with when_some.
.error_frame(on: bool)Whether an error also frames the control in the danger color; on by default. Off, only the message below the control shows the error.
.required()Adds an asterisk to the label and reports the control as required. The application still checks the value.
.when(condition: bool, f: impl FnOnce(Self)Applies f when condition is true.
.when_some(value: Option<T>, f: impl FnOnce(Self, T)Applies f to the value when there is one.
.children(f: impl FnOnce(&mut Ui<'a>)Places the field with the control declared by f, and returns the result of f.