Skip to content

[Validation] Form labels with the DisplayName and Label components #68487

Description

@oroztocil

Scenario contact: @ilonatommy

Scenario

The DisplayName and Label components render a property's display name from its [Display] or [DisplayName] attribute, so field wording lives on the model instead of being repeated in markup. Label renders a <label> element and associates it with its input, either by wrapping the input or by matching for to the input's generated id. This validates both components on simple and nested models, and with localized display names.

Note that Label and DisplayName are new components, distinct from the older InputBase.DisplayName parameter.

Minimum build

.NET 11 Preview 7 or later.

Configurations to cover

  • Blazor Web App
    • Static SSR
    • Interactive Server
    • Interactive WebAssembly
    • Interactive Auto
  • Standalone WebAssembly
  • Hybrid (MAUI)

Also exercise

  • Published output
  • An existing .NET 10 app upgraded to .NET 11
  • Trimming or ahead-of-time compilation
  • More than one server instance, or a proxy in front
  • Hot Reload
  • An IDE as well as the command line
  • Container

Setup

For the localization part you need resource files for two cultures, one of which is not your machine's own, and a way to switch culture at runtime.

What to build

Two models:

  • A flat one, such as a product, where some properties carry [Display(Name = "...")], others carry [DisplayName("...")], and one carries neither.
  • One with a nested object, such as an order containing a delivery address, so some fields belong to a child branch.

Over them:

  • An edit form using Label in both patterns: wrapping the input for some fields, and standalone with for/id for others. Include fields from the nested branch.
  • A table whose column headers use DisplayName.
  • Validation on a few fields.

Then point the [Display] attributes at resource files, translate them, and make the culture switchable.

Things to try

  • The same property rendered by DisplayName in a table header and by Label in the form.
  • Clicking labels in both patterns and watching where focus lands.
  • The property with no display attribute.
  • Nested-branch fields, including a property name that also exists on the root.
  • Submitting invalid values and reading how fields are named in the messages.
  • Switching culture and reloading.
  • Localized names supplied with [Display(ResourceType = typeof(MyResources), Name = "...")].
  • Setting an id on an input yourself and seeing what its Label does.

Expected behavior

DisplayName and Label both render the name from the property's metadata, so a table header and a form label for the same property always agree. A property with no attribute falls back to its property name. Label associates with its input in both patterns. Nested fields are addressed independently of same-named root fields. Localized names follow the active culture.

Must hold

  • A property with [Display(Name = "...")] renders that name in both DisplayName and Label.
  • A property with [DisplayName("...")] renders that name.
  • A property with neither attribute renders its property name.
  • Standalone Label renders a for equal to its input's id, and clicking it focuses that input.
  • Wrapping Label renders the input inside the <label>, and clicking the label text focuses it.
  • A nested field and a same-named root field render different id values, each label pointing at its own input.
  • Under the second culture, both components render the translated names.

Expected differences between configurations

  • Inputs rendered on the server, which means static SSR and Interactive Server, also render a name attribute so the form can post. Inputs rendered in the browser, which means Interactive WebAssembly and Standalone WebAssembly, may not. The id that ties a label to its input is present in every configuration.
  • Culture switching may need to be implemented differently for the individual configurations. Note that culture switching is not the scenario tested here, just a setup.

Documentation to use

What to report

Report results using the format described in the validation testing manual. Include link to a repository with the test app.

Metadata

Metadata

Labels

ValidationThis issue is used to track validation effortsarea-blazorIncludes: Blazor, Razor Componentsvalidation-scenarioThis issue describes a validation testing scenario

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions