Blazored / FluentValidation

A library for using FluentValidation with Blazor
https://blazored.github.io/FluentValidation/
MIT License
569 stars 80 forks source link

[Question] GetFailuresFromLastValidation doesn't exist #226

Open GuillemArnauCollell opened 2 months ago

GuillemArnauCollell commented 2 months ago

Good morning,

I've installed the latest version (v. 2.1.0) in a Blazor Server with .NET 7 and the FluentValidationValidator class doesn't have the "GetFailuresFromLastValidation" method.

This is all I got:

using System;
using System.Linq;
using System.Threading.Tasks;
using FluentValidation;
using FluentValidation.Internal;
using FluentValidation.Results;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;

namespace Blazored.FluentValidation;

public class FluentValidationValidator : ComponentBase
{
    [Inject]
    private IServiceProvider ServiceProvider { get; set; }

    [CascadingParameter]
    private EditContext? CurrentEditContext { get; set; }

    [Parameter]
    public IValidator? Validator { get; set; }

    [Parameter]
    public bool DisableAssemblyScanning { get; set; }

    [Parameter]
    public Action<ValidationStrategy<object>>? Options { get; set; }

    internal Action<ValidationStrategy<object>>? ValidateOptions { get; set; }

    public bool Validate(Action<ValidationStrategy<object>>? options = null)
    {
        if (CurrentEditContext == null)
        {
            throw new NullReferenceException("CurrentEditContext");
        }

        ValidateOptions = options;
        try
        {
            return CurrentEditContext.Validate();
        }
        finally
        {
            ValidateOptions = null;
        }
    }

    public async Task<bool> ValidateAsync(Action<ValidationStrategy<object>>? options = null)
    {
        if (CurrentEditContext == null)
        {
            throw new NullReferenceException("CurrentEditContext");
        }

        ValidateOptions = options;
        try
        {
            CurrentEditContext.Validate();
            if (!CurrentEditContext.Properties.TryGetValue("AsyncValidationTask", out object value))
            {
                throw new InvalidOperationException("No pending ValidationResult found");
            }

            await (Task<ValidationResult>)value;
            return !CurrentEditContext.GetValidationMessages().Any();
        }
        finally
        {
            ValidateOptions = null;
        }
    }

    protected override void OnInitialized()
    {
        if (CurrentEditContext == null)
        {
            throw new InvalidOperationException("FluentValidationValidator requires a cascading parameter of type EditContext. For example, you can use FluentValidationValidator inside an EditForm.");
        }

        CurrentEditContext.AddFluentValidation(ServiceProvider, DisableAssemblyScanning, Validator, this);
    }
}

Am I missing something?

Thanks in advance!

nvanexan commented 2 months ago

@GuillemArnauCollell I was just looking for that functionality too.

AFAICT, it looks like a PR was completed to add that functionality here: https://github.com/Blazored/FluentValidation/pull/212

However, there was also subsequent discussion on the approach and a new issue was created: https://github.com/Blazored/FluentValidation/issues/219

Either way, it doesn't look like the package has been published as a new version with the work in #212 yet but the readme is mentioning that work.

@chrissainty are you planning on publishing a release with the #212 work soon? Or are you going to wait for work on issue #219 to be completed?