Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 41 ยท Forms and Validation

Input Components

Use Blazor's built-in input components: InputText, InputNumber, InputDate, InputCheckbox, InputSelect, InputRadioGroup and InputTextArea, with type conversion, formats, CSS classes and extra attributes.

Inside an EditForm you use Blazor's own input components instead of plain <input> elements. They know about the EditContext: they report changes, show validation state through CSS classes and convert between text and the property type (numbers, dates, enums) for you.

The .razor markup is read-only here because Blazor cannot run on this site. The runnable examples are C# stand-ins for the type-conversion and CSS-class logic, not the real components.

The family

Component Renders Property type
InputText <input type="text"> string
InputTextArea <textarea> string
InputNumber<T> <input type="number"> int, long, short, float, double, decimal
InputDate<T> <input type="date"> DateTime, DateOnly, DateTimeOffset, TimeOnly
InputCheckbox <input type="checkbox"> bool
InputSelect<T> <select> any type with text conversion, including enum
InputRadioGroup<T> + InputRadio radio buttons like InputSelect
InputFile file picker see file uploads

All of them use @bind-Value, exactly like the two-way binding of a child component.

A form using most of them

HTML
<EditForm Model="order" OnValidSubmit="Place">
    <p>
        <label>Name <InputText @bind-Value="order.Name" /></label>
    </p>
    <p>
        <label>Quantity <InputNumber @bind-Value="order.Quantity" min="1" max="99" /></label>
    </p>
    <p>
        <label>Deliver on <InputDate @bind-Value="order.DeliveryDate" /></label>
    </p>
    <p>
        <label>Size
            <InputSelect @bind-Value="order.Size">
                <option value="">Choose...</option>
                @foreach (var size in Enum.GetValues<ShirtSize>())
                {
                    <option value="@size">@size</option>
                }
            </InputSelect>
        </label>
    </p>
    <p>
        <label><InputCheckbox @bind-Value="order.Gift" /> This is a gift</label>
    </p>
    <p>
        <label>Note<br />
            <InputTextArea @bind-Value="order.Note" rows="3" />
        </label>
    </p>
    <button>Order</button>
</EditForm>

@code {
    private Order order = new() { Quantity = 1 };

    public enum ShirtSize { Small, Medium, Large }

    public class Order
    {
        public string Name { get; set; } = "";
        public int Quantity { get; set; }
        public DateOnly DeliveryDate { get; set; }
        public ShirtSize Size { get; set; }
        public bool Gift { get; set; }
        public string Note { get; set; } = "";
    }

    private void Place() { }
}

Points to notice:

  • Extra attributes such as min, max, rows, class and placeholder are passed through to the HTML element (the components capture unmatched attributes).
  • InputNumber<int> infers TValue from the bound property. If it cannot, write <InputNumber TValue="int" ...>.
  • InputSelect with an enum: option values are the enum names, and the component parses them back.

Radio buttons

Radio buttons share a group name. Wrap them in InputRadioGroup so they bind to one property:

HTML
<InputRadioGroup @bind-Value="order.Shipping">
    @foreach (var option in Enum.GetValues<ShippingMethod>())
    {
        <label>
            <InputRadio Value="option" /> @option
        </label>
    }
</InputRadioGroup>

What happens when the text is not a number

If a user types abc into an InputNumber<int>, parsing fails. The component does not write a value to the model. It records a validation message, by default The Quantity field must be a number., and marks the field invalid. The same applies to dates and enum values. You can change the text with the ParsingErrorMessage parameter, which uses for the field name:

HTML
<InputNumber @bind-Value="order.Quantity" ParsingErrorMessage="Enter a whole number for {0}." />

Formatting

Inputs of type number and date use the invariant, browser-friendly formats. InputDate accepts a ParsingErrorMessage too, and InputNumber accepts step as plain HTML. For a custom display format on text inputs you need a binding format on a plain <input> instead.

When does the model update?

The built-in components update the model on the change event (when the field loses focus or the user presses Enter), like @bind. To update while typing, use @bind-Value:event="oninput" on the component:

HTML
<InputText @bind-Value="order.Name" @bind-Value:event="oninput" />

Each keystroke then triggers a field change and (if you validate on change) a validation run. That is nice for live feedback and costly for large forms on a slow connection, because Interactive Server sends every keystroke over the network.

Validation CSS classes

Every input component adds one of these classes to its element, based on the EditContext:

  • modified: the user changed this field.
  • valid or invalid: validation result for the field (combined with modified).

So a field the user edited and got wrong has modified invalid. The default Blazor template styles .invalid with a red outline. You can change it in app.css:

CSS
.modified.valid   { outline: 1px solid #26b050; }
.invalid          { outline: 1px solid #e50000; }
.validation-message { color: #e50000; }

Stand-in: parsing text into a typed property

C# Stand-in: TryParseValueFromString

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Stand-in: the CSS state classes

C# Stand-in: modified, valid and invalid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Common mistakes

  • Using a raw <input @bind="..."> inside an EditForm and wondering why no validation message or CSS class appears: it is not connected to the EditContext.
  • Binding InputNumber to a string property.
  • Binding InputSelect to a nullable enum without an empty option.
  • Putting @bind-Value on InputCheckbox for a bool? and being surprised that it stays null until clicked.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which component binds a bool property to a checkbox inside an EditForm?

    InputCheckbox is the built-in component for bool values.
  2. 2. A user types 'abc' into an InputNumber<int>. What happens to the model?

    Parsing failure leaves the model unchanged and records a validation message (ParsingErrorMessage).
  3. 3. Which CSS classes do input components add based on the EditContext?

    modified is combined with valid or invalid so you can style edited and failed fields.
  4. 4. How do you make an InputText update the model on every keystroke?

    The :event modifier chooses oninput instead of the default onchange.

Summary

  • Use InputText, InputNumber<T>, InputDate<T>, InputCheckbox, InputSelect<T>, InputRadioGroup<T> and InputTextArea inside an EditForm.
  • They convert text to the property type and report failures as validation messages.
  • Extra HTML attributes pass through; modified, valid, invalid classes are added automatically.
  • Use :event="oninput" for live updates.

Next: Validation with Data Annotations