UI Toolbox logoUI Toolbox

FormField

A FormField is a wrapper for one input of a form. It shows a label, the input, a description under the input, and an error text. It works with each Unity field and each toolbox field.

A form with the legend New character: a name field with a description, a class combo box, three number fields and a star rating with a description

How to make a form with a description, an error and a group of fields:

<tb:FormField name="userName" label="User name" description="Other players see this name.">
    <ui:TextField />
</tb:FormField>
<tb:FormField label="Music" description="The volume of the music." horizontal="true">
    <ui:Slider low-value="0" high-value="100" />
</tb:FormField>

<ui:GroupBox text="Notifications" class="tb-field-set">
    <tb:FormField description="Send a mail when a build fails.">
        <ui:Toggle text="Build mails" />
    </tb:FormField>
    <tb:FormField description="Show a message when a friend comes online.">
        <tb:Switch label="Friends" />
    </tb:FormField>
</ui:GroupBox>
var userName = root.Q<FormField>("userName");
var input = userName.Q<TextField>();
saveButton.clicked += () =>
{
    userName.Error = string.IsNullOrWhiteSpace(input.value) ? "Enter a user name." : "";
    if (!FieldError.Has(userName)) save();
};
input.RegisterValueChangedCallback(_ => userName.Error = "");
Member Description
Label (label) The text above the input, or at its left when Horizontal. An empty label shows no line.
Description (description) The muted help text under the input. Rich text tags work. An empty description shows no line.
Error (error) The error text under the description. A text also gives the label and the border of the input the danger color. An empty text removes the error.
Horizontal (horizontal) Puts the label at the left of the input. False by default.
FormField.SET_CLASS The class tb-field-set for a Unity GroupBox: a group of fields with a border. The text of the group box is the legend.

Tips:

Styling

Class Element
tb-form-field The wrapper, with a --tb-spacing-md margin under it.
tb-form-field--horizontal Set by Horizontal: the label is at the left, 120 px wide.
tb-form-field__label The bold label. Hidden without a text.
tb-form-field__body The column with the input, the description and the error.
tb-form-field__content The parent of the input.
tb-form-field__description The description in --tb-color-text-muted and the small font size. Hidden without a text.
tb-form-field__error The error text in --tb-color-danger and the small font size. Hidden without a text.
tb-field--error Set on the wrapper while Error has a text.
tb-field-set A GroupBox as a group of fields: a --tb-color-border border, --tb-radius-md corners and a bold legend. For the title on the border, use tb-group-box.
.tb-form-field--horizontal > .tb-form-field__label { width: 200px; }