Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] GetFailuresFromLastValidation doesn't exist #226

Open
GuillemArnauCollell opened this issue May 9, 2024 · 2 comments
Open

[Question] GetFailuresFromLastValidation doesn't exist #226

GuillemArnauCollell opened this issue May 9, 2024 · 2 comments
Labels
Question Question about this project Triage Issue needs to be triaged

Comments

@GuillemArnauCollell
Copy link

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!

@GuillemArnauCollell GuillemArnauCollell added Question Question about this project Triage Issue needs to be triaged labels May 9, 2024
@nvanexan
Copy link

@GuillemArnauCollell I was just looking for that functionality too.

AFAICT, it looks like a PR was completed to add that functionality here:
#212

However, there was also subsequent discussion on the approach and a new issue was created:
#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?

@Jelle-infra
Copy link

Jelle-infra commented Jul 30, 2024

hi,

I had the same issue in v2.1.0, I upgraded to v2.2.0, in this version the method is available. But I get the error:

image

when GetFailuresFromLastValidation() is called.
I use version 11.9.2 for the Fluentvalidation and FluentValidation.DependencyInjectionExtension (latest version at the time of writing this). Is this another bug in the library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Question about this project Triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

3 participants