LgValidator
LgValidator allows you to add custom validation errors
(business rules, errors returned by the server, etc.) to an
LgEditForm, field by field. Errors are attached
to the form's EditContext: they display under the relevant fields
and in the toastr, according to the form's display options.
<LgEditForm Model="_model" OnValidSubmit="OnValidSubmitAsync">
<LgValidator @ref="_validator" />
...
</LgEditForm>
- Namespace:
Lagoon.UI.Components - Inherits from:
LgValidationBase→LgComponentBase - NuGet package:
Lagoon.UI
The component must be placed inside a form (it requires the cascading
parameter EditContext, provided by LgEditForm); otherwise an exception
is raised during initialization.
Usage
Adding custom business errors on submission
Retrieve the component with @ref, add errors with AddError
(field name + message or dictionary key), then call
ThrowPendingErrors() to display them.
<LgEditForm Model="_model" OnValidSubmit="OnValidSubmitAsync">
<LgValidator @ref="_validator" Title="#OrderValidationErrors" />
<LgTextBox Label="#Reference" @bind-Value="_model.Reference" />
<LgDateBox Label="#DeliveryDate" @bind-Value="_model.DeliveryDate" />
<LgButton Text="#Save" IsSubmit="true" />
</LgEditForm>
public partial class OrderEditPage : LgPage
{
// Validator reference: allows adding business errors to the form.
private LgValidator _validator;
private readonly OrderVm _model = new();
// Triggered when DataAnnotations are valid: then applies business rules.
private Task OnValidSubmitAsync(EditContext editContext)
{
_validator.ClearErrors();
if (_model.DeliveryDate < DateTime.Today)
{
_validator.AddError(nameof(OrderVm.DeliveryDate), "#DeliveryDateInPast");
}
if (_validator.HasError)
{
_validator.ThrowPendingErrors();
return Task.CompletedTask;
}
ShowSuccess("#OrderSaved");
return Task.CompletedTask;
}
private sealed class OrderVm
{
public string Reference { get; set; }
public DateTime? DeliveryDate { get; set; }
}
}
Displaying errors returned by the server
SetErrorList replaces the error list with a dictionary
of "field → messages" (for example deserialized from an API response);
AddErrorList adds them to existing errors.
public partial class CustomerEditPage : LgPage
{
private LgValidator _validator;
private readonly CustomerVm _model = new();
// Sends the model to the server and displays the validation errors returned, field by field.
private async Task OnValidSubmitAsync(EditContext editContext)
{
Dictionary<string, List<string>> serverErrors = await SaveAsync(_model);
if (serverErrors.Count > 0)
{
_validator.SetErrorList(serverErrors);
_validator.ThrowPendingErrors();
return;
}
ShowSuccess("#CustomerSaved");
}
private static Task<Dictionary<string, List<string>>> SaveAsync(CustomerVm model)
{
return Task.FromResult(new Dictionary<string, List<string>>());
}
private sealed class CustomerVm
{
public string Name { get; set; }
}
}
The display mode (message under the field, summary at the top of the form,
generic or detailed toastr) comes from the options of the parent LgEditForm or,
if not specified, from the application's behavior configuration
(EditFormErrorsDisplayOptions). The summary at the top of the form is rendered
by LgValidationSummary, automatically added by LgEditForm — you
do not need to declare it yourself.
Properties
All [Parameter] properties of the component, including those inherited.
| Name | Type | Default | Declared in | Description |
|---|---|---|---|---|
Title | string | null | LgValidator | Title of the error message displayed in the toastr (default generic message). |
CssClass | string | null | LgValidationBase | Additional CSS classes. |
LgValidator consumes the EditContext and error display configuration
(LgEditFormConfiguration) provided by the parent LgEditForm.
Methods & events
| Member | Signature | Description |
|---|---|---|
HasError | bool | true if at least one error is pending display. |
AddError | void AddError(string fieldName, string errorMessage) | Adds an error message (text or dictionary key) for a field. |
AddErrorList | void AddErrorList(Dictionary<string, List<string>> errors) | Adds a set of errors "field → messages". |
SetErrorList | void SetErrorList(Dictionary<string, List<string>> errors) | Replaces current errors with the provided set. |
ThrowPendingErrors | void ThrowPendingErrors(bool showInToastr = true) | Attaches pending errors to fields and displays them (optional toastr). |
ClearErrors | void ClearErrors() | Clears all errors. |
Best practices / accessibility
- Call
ClearErrors()at the start of processing before adding errors from the new validation pass. - Use
nameof(...)for the field name: it must match the name of the bound property for the message to display under the correct field. - Messages accept dictionary keys (
#MyKey): add the key to theDico.xmlof the project that owns the message.
All parameters: API Reference — LgValidator.
Related components
LgEditForm— edit form that provides theEditContext.