The BaseValidator component serves as the abstract base class for all validation controls in BlazorWebFormsComponents. It provides the shared properties, lifecycle, and integration with Blazor's EditForm validation system that all validator components inherit.
Original Microsoft documentation for ASP.NET BaseValidator: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.basevalidator?view=netframework-4.8
BaseValidator is not used directly in markup—instead, you use its concrete implementations:
- RequiredFieldValidator — validates that a field has a value
- CompareValidator — compares a field value to a constant or another field
- RangeValidator — validates that a value falls within a range
- RegularExpressionValidator — validates against a pattern
- CustomValidator — calls a custom validation function
All validators inherit the same set of properties, display modes, and validation group support from BaseValidator.
Reference to the input control to validate. You can specify this in two ways:
-
String ID (Web Forms style):
<InputText @bind-Value="model.Name" /> <RequiredFieldValidator ControlToValidate="Name" Text="Name is required" />
The string matches the property name on your EditForm model.
-
ForwardRef (Blazor native):
<InputText @ref="NameInput.Current" @bind-Value="model.Name" /> <RequiredFieldValidator ControlRef="@NameInput" Text="Name is required" /> @code { ForwardRef<InputBase<string>> NameInput = new ForwardRef<InputBase<string>>(); }
When both
ControlToValidateandControlRefare set,ControlReftakes precedence.
Controls how the error message is displayed:
| Value | Behavior |
|---|---|
| Static (default) | Error message always takes up space (visibility: hidden when valid) |
| Dynamic | Error message only appears when invalid (display: none when valid) |
| None | Error message never displayed |
<RequiredFieldValidator Display="ValidatorDisplay.Dynamic" Text="Name is required" />The error message displayed inline next to the validator. Shown when validation fails.
<RequiredFieldValidator Text="This field is required" />The error message displayed in the ValidationSummary component. Can be different from Text.
<RequiredFieldValidator ErrorMessage="Name field is required" />Groups validators for selective validation. When a button with a matching ValidationGroup is clicked, only validators in that group validate. Requires wrapping your form in <ValidationGroupProvider>.
<ValidationGroupProvider>
<EditForm Model="@model">
<InputText @ref="NameInput.Current" @bind-Value="model.Name" />
<RequiredFieldValidator ControlRef="@NameInput"
Text="Name is required"
ValidationGroup="Personal" />
</EditForm>
</ValidationGroupProvider>Enable or disable the validator. When disabled, the validator does not perform validation.
<RequiredFieldValidator Enabled="@isEnabled" Text="Name is required" />BaseValidator inherits from BaseStyledComponent and supports all standard Web Forms styling:
ForeColor— text color of the error messageBackColor— background colorCssClass— CSS class name(s)Font.Bold,Font.Italic,Font.Name,Font.Size,Font.Strikeout,Font.Underline— font styling
<RequiredFieldValidator ForeColor="Red" CssClass="validation-error" Text="Required" />A read-only property that indicates whether the field is currently valid. Useful for conditionally rendering UI based on validation state.
@if (!myValidator.IsValid)
{
<p>Please fix the validation errors above.</p>
}
@code {
RequiredFieldValidator myValidator;
}BaseValidator integrates with Blazor's EditForm validation system:
- Registration — When a validator initializes, it registers with the cascading EditContext
- Validation Request — When the form validates (e.g., button submit), the EditContext fires
OnValidationRequested - Value Resolution — The validator retrieves the current value from the input control
- Validation — The validator checks the value and updates the message store
- State Notification — The EditContext is notified of validation state changes
- Cleanup — When the validator is disposed, it unregisters from the EditContext
All validators require a cascading EditContext, typically provided by the EditForm component:
<EditForm Model="@model">
<InputText @bind-Value="model.Name" />
<RequiredFieldValidator Text="Name is required" />
</EditForm>The EditContext coordinates validation across all validators on the form.
=== "Web Forms"
```html
<asp:RequiredFieldValidator
ControlToValidate="NameInput"
Display="Dynamic"
ErrorMessage="Name is required"
Text="Name is required"
ForeColor="Red"
ValidationGroup="Personal"
runat="server" />
```
=== "Blazor"
```razor
<RequiredFieldValidator
ControlToValidate="Name"
Display="ValidatorDisplay.Dynamic"
ErrorMessage="Name is required"
Text="Name is required"
ForeColor="Red"
ValidationGroup="Personal" />
```
Key Differences:
ControlToValidatein Blazor uses the model property name instead of a control IDDisplayuses theValidatorDisplayenum instead of string values- No
runat="server"needed - Optional
ControlRefparameter for Blazor-native ForwardRef approach - Removed:
EnableClientScript,SetFocusOnError(Web Forms client-side scripting features not needed in Blazor)
BaseValidator has the following concrete implementations:
- RequiredFieldValidator — validates that a field has a value
- CompareValidator — compares a field value to a constant or another field
- RangeValidator — validates that a value falls within a range
- RegularExpressionValidator — validates against a pattern
- CustomValidator — calls a custom validation function
See the individual validator pages for specific usage examples.
- RequiredFieldValidator — validates that a field has a value
- CompareValidator — compares field values
- RangeValidator — validates value ranges
- RegularExpressionValidator — pattern validation
- CustomValidator — custom validation logic
- ValidationSummary — display validation errors