1 Input field
Byron edited this page 2026-10-02 23:00:12 +02:00

Basic

The <CleanInputField /> component automatically adds a <label> element, bound to your input, containing the value in the [Display] attribute of the property you give it. It also automatically shows the current error-message for the given property, if it's a child of an <EditForm /> component.

@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Components.Forms

<EditForm Model="@(Model)" FormName="@(formName)">
	<DataAnnotationsValidator />

	<CleanInputField Property="@(() => Model.Username)">
		<CleanInputText @bind-Value="@(Model.Username)" />
	</CleanInputField>

	<CleanInputField Property="@(() => Model.Password)">
		<ChildContent>
			<CleanInputText @bind-Value="@(Model.Password)" type="password" />
		</ChildContent>

		<HelpContent>
			Please try to make your password have at least 8 characters!
		</HelpContent>
	</CleanInputField>

	<CleanButton ButtonType="@(CleanButtonType.Primary)" type="submit">
		Sign in
	</CleanButton>
</EditForm>

@code {

	private const string formName = "MyForm;

	[SupplyParameterFromForm(FormName = Basic.formName)]
	private BasicModel Model { get; set; } = new();

	private sealed class BasicModel
	{
		[Required]
		[Display(Name = "Your username")]
		public string? Username { get; set; }

		[Required]
		[Display(Name = "Your super secure password")]
		public string? Password { get; set; }
	}

}

Inline

@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Components.Forms

<EditForm Model="@(Model)" FormName="@(formName)">
	<DataAnnotationsValidator />

	<CleanInputField Property="@(() => Model.Username)" Inline>
		<CleanInputText @bind-Value="@(Model.Username)" />
	</CleanInputField>

	<CleanInputField Property="@(() => Model.Password)" Inline>
		<ChildContent>
			<CleanInputText @bind-Value="@(Model.Password)" type="password" />
		</ChildContent>

		<HelpContent>
			Please try to make your password have at least 8 characters!
		</HelpContent>
	</CleanInputField>

	<CleanButton ButtonType="@(CleanButtonType.Primary)" type="submit">
		Sign in
	</CleanButton>
</EditForm>

@code {

	private const string formName = "MyForm";

	[SupplyParameterFromForm(FormName = Inline.formName)]
	private BasicModel Model { get; set; } = new();

	private sealed class BasicModel
	{
		[Required] public string? Username { get; set; }

		[Required] public string? Password { get; set; }
	}

}

Manual label

@using System.ComponentModel.DataAnnotations

<CleanInputField Property="@(() => model.Username)">
	<LabelContent>
		<i>Your</i> <b>username</b>:
	</LabelContent>

	<ChildContent>
		<CleanInputText @bind-Value="@(model.Username)" />
	</ChildContent>
</CleanInputField>

@code {

	private readonly BasicModel model = new();

	private sealed class BasicModel
	{
		[Required] public string? Username { get; set; }
	}

}

Manual error message

@using System.ComponentModel.DataAnnotations

<CleanInputField Property="@(() => model.Username)">
	<ChildContent>
		<CleanInputText @bind-Value="@(model.Username)" />
	</ChildContent>
	
	<ErrorContent>
		Please enter your username.
	</ErrorContent>
</CleanInputField>

@code {

	private readonly BasicModel model = new();

	private sealed class BasicModel
	{
		[Required] public string? Username { get; set; }
	}

}