Skip to main content

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.

OrderEditPage.razor
<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>
OrderEditPage.razor.cs
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.

CustomerEditPage.razor.cs
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; }
}
}
Error display

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.

NameTypeDefaultDeclared inDescription
TitlestringnullLgValidatorTitle of the error message displayed in the toastr (default generic message).
CssClassstringnullLgValidationBaseAdditional CSS classes.
Cascading parameters

LgValidator consumes the EditContext and error display configuration (LgEditFormConfiguration) provided by the parent LgEditForm.

Methods & events​

MemberSignatureDescription
HasErrorbooltrue if at least one error is pending display.
AddErrorvoid AddError(string fieldName, string errorMessage)Adds an error message (text or dictionary key) for a field.
AddErrorListvoid AddErrorList(Dictionary<string, List<string>> errors)Adds a set of errors "field → messages".
SetErrorListvoid SetErrorList(Dictionary<string, List<string>> errors)Replaces current errors with the provided set.
ThrowPendingErrorsvoid ThrowPendingErrors(bool showInToastr = true)Attaches pending errors to fields and displays them (optional toastr).
ClearErrorsvoid 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 the Dico.xml of the project that owns the message.
Complete reference
  • LgEditForm — edit form that provides the EditContext.