Fields

Editable fields, display fields and read-only values on a form

Introduction

A field allows you to edit a property of your resource on a form. To add a field, call Add() on the fields builder, with an expression that selects the property. The property must have a public getter and a public setter. If it doesn't have a public setter, StellarAdmin leaves the field out and reports a configuration problem.

Configuring a field

The Add() method can take an optional configure parameter which contains a field builder that allows you to specify additional configuration for the field. The second field in the example below uses the field builder to override the field's default title (which displays in the label).

resource.AllowEdit(edit => edit.Fields(fields =>
{
    fields.Add(booking => booking.TravellerId);
    fields.Add(booking => booking.DepartureId, field => field.Title = "Departs on");
}));

Setting the label and help text

You can override a field's label the help text that displays below the field using the field builder's Title and Description properties.

By default, the Title is the property name split into words, for example "Start Date" for StartDate. You can override the title either by using the [Display(Name)] attribute on the property or by specifying the Title on the field builder. The Title on the field builder takes precedence.

A field that references another resource is labeled with that resource's label unless the field sets a Title or the property has a [Display(Name)].

Validation error messages use the field's label, so they always match it, whether the label comes from Title, [Display(Name)] or the property name. Messages that a validation library such as FluentValidation writes itself use that library's own names.

The help text uses the property's [Display(Description)] attribute value, but can be overridden by using the Description property of the field builder.

The example below sets the label and the help text of a departure's StartDate:

public class Departure
{
    [Display(Name = "Start date", Description = "The first day of the trip.")]
    public DateOnly StartDate { get; set; }
}
The Start date field with its label and the help text The first day of the trip

Making fields read-only or disabled

To display a field's editor but stop people from changing its value, make the field read-only or disabled. The example below configures Reference as read-only, Total read-only once a booking is confirmed, and disables Notes:

resource.AllowEdit(edit => edit.Fields(fields =>
{
    fields.Add(booking => booking.Reference).ReadOnly();
    fields.Add(booking => booking.TotalAmount).ReadOnly(booking => booking.Status == BookingStatus.Confirmed);
    fields.Add(booking => booking.SpecialRequests).Disabled();
}));
A booking's edit page with a read-only reference and faded, disabled special requests

Read only fields visually look the same as an editable field and can receive keyboard focus, but the user will not be able to edit the value. Disabled fields appears grayed out and will not receive keyboard focus when the user tabs through the field.

StellarAdmin ignores any value posted for a read-only or disabled field, so nobody can change the value by editing the page's HTML. A field that is both read-only and disabled appears as disabled.

The ReadOnly(condition) overload allows you to specify an expression that determines whether the field is read-only. The expression is evaluated when the form loads, so the field doesn't change while someone edits other fields.

You can also use the [ReadOnly(true)] or [Editable(false)] attributes on a property to make it read-only.

Displaying non-editable fields

Both read-only and disabled fields still display the field's editor. However, StellarAdmin also supports the concept of display fields, which renders a value on the form without an editor. Use it for values nobody edits, such as a reference number or the date a record was created.

A booking's edit page with its reference, booking date, traveller's email and total as text, and an editable status

To add a display field, call AddDisplayField(). The example below adds four display fields and one editable field:

using StellarAdmin.Resources.Displays;

resource.AllowEdit(edit => edit.Fields(fields =>
{
    fields.AddDisplayField(booking => booking.Reference);
    fields.AddDisplayField(booking => booking.BookedAt, field => field.Title = "Booked on");
    fields.AddDisplayField(booking => booking.Traveller.Email);
    fields.AddDisplayField(booking => booking.TotalAmount)
        .UseDisplay<NumberDisplay>(number => number.Format = "C");
    fields.Add(booking => booking.Status);
}));

As opposed to fields bound to an editor, the property of a display field only requires a public getter. You can also use the property of a referenced resource, such as booking.Traveller.Email. For an EF Core resource, StellarAdmin loads the related record.

The following members are available on the display field builder.

MemberDescription
TitleSets the label. The default is the same as for an editable field.
EmptyTextSets the text for an empty value. The default is the property's [DisplayFormat(NullDisplayText)], or a dash.
OrientationPlaces the label above the value with DisplayFieldOrientation.Vertical, the default, or beside it with Horizontal.
UseDisplay<TDisplay>()Renders the value with a different display type.

You can also display a computed value as a display property, such as the price per traveller. A computed value has no property to take its label from, so you must set its Title.

The example below computes the price per traveller and uses a number display:

fields.AddDisplayField(booking => booking.TotalAmount / booking.Passengers.Count, field =>
{
    field.Title = "Per traveller";
    field.UseDisplay<NumberDisplay>(number => number.Format = "C");
});

Display a list of display fields

You can add a list of display fields to a form using AddDetails(). StellarAdmin renders the fields as a list of labels and values. The example below places the booking's editable fields in two columns on the left, and a History card with two display fields in a narrow column on the right:

using StellarAdmin.UI;

resource.AllowEdit(edit => edit.Fields(fields =>
{
    //... some code omitted for brevity

    fields.AddDetails("History", details =>
    {
        details.Layout = FormSectionLayout.Card;
        details.Variant = DetailsVariant.Divided;
        details.Add(booking => booking.BookedAt);
        details.Add(booking => booking.Reference);
    });
}));
A booking's edit page with its fields on the left and a History card showing when it was booked and its reference on the right

The details builder has the following members:

MemberDescription
DescriptionSets the text below the section's title.
LayoutSets the section to one of the section layouts.
OrientationPlaces each label above its value with DetailsOrientation.Vertical, the default, or beside it with Horizontal.
VariantLays out the fields in a grid with DetailsVariant.Default, the default, or in rows with a line between them with Divided.
Add()Adds a display field, with the same settings as AddDisplayField().
Columns()Sets the number of columns.

On this page