Alex Ivan
Alex Ivan

Reputation: 95

Blazor WASM - Fluent Validation

I have the following rules created in my custom validator :

  public class AddInvoiceParameterCommandValidator : AbstractValidator<AddInvoiceParameterCommand>
{
    public AddInvoiceParameterCommandValidator(IStringLocalizer<AddInvoiceParameterCommandValidator> localizer)
    {
        RuleFor(p => p.Start)
            .Must(x => x > 0)
            .WithMessage(x => localizer["Start must be greater than 0!"]);

        RuleFor(p => p.Prefix)
            .Must(x => !string.IsNullOrWhiteSpace(x))
            .WithMessage(x => localizer["Prefix is required!"])
            .MaximumLength(5)
            .WithMessage(x => localizer["Length must not exceed"] + " 5 characters");

        RuleFor(p => p.End)
            .Must(x => x > 0)
            .WithMessage(x => localizer["End must be greater than 0!"]);

        RuleFor(x => x)
            .Must(StartMustBeLessThanEnd)
            .WithMessage(localizer["Start must be less than end!"]);

    }

    private bool StartMustBeLessThanEnd(AddInvoiceParameterCommand parameter)
    {
        return parameter.Start < parameter.End;
    }
}

I have no issues for Start and End, as I get the message in the front-end form, when they are not greater than 0. Problem is, how can I display the message in the front end, for the "Start must be less than end" rule?

This is my current code for the modal I am using : (Blazor WASM + MudBlazor)

@inject Microsoft.Extensions.Localization.IStringLocalizer<AddInvoiceParameterModal> _localizer

    
<EditForm Model="@AddInvoiceParameterModel" OnValidSubmit="SaveAsync">
    <FluentValidationValidator @ref="_fluentValidationValidator" />
    <MudDialog>
        <TitleContent>
              <MudText Typo="Typo.h6">
                        <MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-3 mb-n1" />
                        @_localizer["Add Parameter"]
                    </MudText>
        </TitleContent>
        <DialogContent>
            <MudGrid>
                <MudItem xs="12" md="6">
                    <MudTextField T="int" For="@(() => AddInvoiceParameterModel.Start)" @bind-Value="AddInvoiceParameterModel.Start" Label="@_localizer["Start"]" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField T="int" For="@(() => AddInvoiceParameterModel.End)" @bind-Value="AddInvoiceParameterModel.End" Label="@_localizer["End"]" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField T="string" For="@(() => AddInvoiceParameterModel.Prefix)" @bind-Value="AddInvoiceParameterModel.Prefix" Label="@_localizer["Prefix"]" />
                </MudItem>
            </MudGrid>
        </DialogContent>
        <DialogActions>
            <MudButton DisableElevation Variant="Variant.Filled" OnClick="Cancel">@_localizer["Cancel"]</MudButton>
                <MudButton DisableElevation Variant="Variant.Filled" ButtonType="ButtonType.Submit" Disabled="@(!Validated)" Color="Color.Success">@_localizer["Save"]</MudButton>
        </DialogActions>
    </MudDialog>
</EditForm>

Upvotes: 0

Views: 1150

Answers (1)

Steve Greene
Steve Greene

Reputation: 12304

You could either do this to show message with start:

RuleFor(p => p.Start)
    .Must(x => x > 0).WithMessage(x => localizer["Start must be greater than 0!"])
    .Must(StartMustBeLessThanEnd).WithMessage(localizer["Start must be less than end!"]);

Or you can add a summary control that show's messages not affiliated with a property:

<EditForm Model="@AddInvoiceParameterModel" OnValidSubmit="SaveAsync">
    <FluentValidationValidator @ref="_fluentValidationValidator" />

    [put this where you want displayed ]
    <Microsoft.AspNetCore.Components.Forms.ValidationSummary />

Upvotes: 1

Related Questions