'FluentValidation trigger validation on each field in form

So in my Blazor-Server app i have added FluentValidation for individual input components and that works fine. My next task is to also trigger the validation rules for all the components in the form. Currently rules are triggered when the user interacts with the components input only, i want to also trigger those same rules when the user clicks on the submit button on the form.

TextField component:

public partial class TextField
    {

        ValidationResult Validation = new ValidationResult();

        [Parameter]
        public AbstractValidator<TextField> Rules { get; set; } = null;

        public void Validate()
        {
            Validation = Rules.Validate(this);
        }

        private void OnInputEvent(ChangeEventArgs changeEvent)
        {
            Value = (string)changeEvent.Value;
            if (Rules != null)
            {
                Validate();
            }
            
        }

        private void OnInputEvent(FocusEventArgs changeEvent)
        {
            if (Rules != null)
            {
                Validate();
            }
        }

    }

Login Page:

<TextField class="mb-3" Name="User Name" Width="190px" Rules="UsernameRules" spellcheck="false" />
<TextField class="mb-3" Name="Password" Width="190px" type="password" />

<Button class="text-light" Height="30px" Width="260px" Color="--accent" @onclick="Validate">Log In</Button>

Login Page code-behind:

public partial class Login
{

    private async void Validate()
    {
        <------------ HOW DO I TRIGGER ALL THE RULES FOR EACH TEXTFIELD COMPONENT?? -------------
    }

    UsernameValidation UsernameRules = new UsernameValidation();
    private class UsernameValidation : AbstractValidator<TextField>
    {
        public UsernameValidation()
        {

            RuleFor(field => field.Value)
                .NotEmpty().WithMessage("Please enter a username");

        }

    }

}


Solution 1:[1]

As long as you are using EditForm you only need to link your edit form action to OnValidSubmit. You're probably currently linking to OnSubmit which bypasses running _editContext.Validate().

Here's the relevant code from EditForm:

    private async Task HandleSubmitAsync()
    {
        Debug.Assert(_editContext != null);

        if (OnSubmit.HasDelegate)
        {
            // When using OnSubmit, the developer takes control of the validation lifecycle
            await OnSubmit.InvokeAsync(_editContext);
        }
        else
        {
            // Otherwise, the system implicitly runs validation on form submission
            var isValid = _editContext.Validate(); // This will likely become ValidateAsync later

            if (isValid && OnValidSubmit.HasDelegate)
            {
                await OnValidSubmit.InvokeAsync(_editContext);
            }

            if (!isValid && OnInvalidSubmit.HasDelegate)
            {
                await OnInvalidSubmit.InvokeAsync(_editContext);
            }
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MrC aka Shaun Curtis