FluentValidation icon indicating copy to clipboard operation
FluentValidation copied to clipboard

[Feature Request] Severity

Open Liero opened this issue 4 years ago • 4 comments
trafficstars

Is your feature request related to a problem? Please describe. FluentValidator supports Severity levels for validation messages.

I only want to prevent form submit, when there are ValidationFailures with severity Error and ignore Warnings and Infos. Currently, the FluentValidator generates errors for all severity levels. I would like to set minimum level to Error and ignore Warning and Info.

Describe the solution you'd like

<FluentValidator ValidationFailureFilter="@(error => error.Severity >= Severity.Error)" >

Describe alternatives you've considered

<FluentValidator Severity="Severity.Error" >

Additional context

Even when I want to enable submit when there are warnings, I still want to be able to show the warnings in the ValidationSummary.

That might be tricky, but in worst case the FluentValidator might expose Validate, resp ValidateAsync method.

FluentValidator _fluentValidatorRef;

async Task OnSubmit()
{
  var result = await _fluentValidatorRef.ValidateAsync();
  bool isValid = !result.Errors.Any(e => e.Severity >= Severity.Error);
  if (isValid) 
  {
      .....
  }
}

Alternatively, FluentValidator might expose ValidationResult, but it will propagate to EditContext only those errors, that matches the ValidationFailureFilter. We will be able to catch the Warnings and show them somewhere, but I prefer the custom OnSubmit logic

Liero avatar Jul 06 '21 08:07 Liero

Hi @Liero, thanks for raising this. I'm really swamped at the moment but I'm planning on looking over this proposal on Friday (16th). I've not worked with severity levels in FluentValidation before so I just need to get up to speed on it.

chrissainty avatar Jul 11 '21 08:07 chrissainty

Once we agree on design proposal, I can post PR.

Concept of Severity in FV is very straightforward - Rules can be configured with Severity level and validation results will get the Severity flag.

Liero avatar Jul 13 '21 06:07 Liero

Hi @Liero,

I've been looking into your request a little and I think we can definitely do this. I haven't had the time I was expecting to go into the details of what a solution would look like. So there are two options. You can suggest something which we can work from. Or you can wait until I get some time, can't tell you when--life is crazy right now.

chrissainty avatar Jul 19 '21 09:07 chrissainty

Since I needed this feature (and other now), I have created my own advanced https://github.com/Liero/vNext.BlazorComponents.FluentValidation. Feel free to use it.

Liero avatar Oct 18 '21 16:10 Liero

@chrissainty I'm a little lost. This used to work, but now Validate() returns a bool only, how can I get to the Severity of a error message now?

EDIT: I looked at the source and, if I'm not wrong, we would need to do something like add a Dictionary<FieldIdentifier, ValidationResult[]> LastResult to FluentValidationsValidator and fill it from the extension method ValidateModel in the foreach-loop, right? Is that something you would accept if I'd create a PR?

Just for you to know: My goal is to be able to make a field yellow or red depending on the severity of a validation error. So I'd need the severity of validation errors in addition to just the text, but only the ones pertaining to a certain FieldIdentifier.

luetm avatar Feb 14 '24 10:02 luetm

@luetm I think this may have been closed in error. I'd be happy to take a PR for the work if you're willing.

chrissainty avatar Feb 17 '24 08:02 chrissainty

@chrissainty https://github.com/Blazored/FluentValidation/pull/212 I took this route of making the whole result available so, if anything besides severity is needed, we don't need to add anything else. Let me know in the PR if the code is acceptable!

luetm avatar Feb 17 '24 11:02 luetm