0

I have a Blazor application. In this application I have several models with various ValidationAttributes.

To validate these models I use an EditForm from Microsoft.AspNetCore.Components.Forms. On this form there is a parameter Called EditContext where I call the Validate() method to validate my models.

The validation itself works fine. However the order of when validations are run seems to be based on the type, like this:

  1. Required
  2. Other(Like Range)
  3. IValidatableObject

This results in Required validations being validated first and only after these are valid in the model the other validations are running.

What I want is for all validations to run at the same time.

Does anyone know how to achieve this in Blazor?

Thanks

  • Have you checked on the guidelines ? https://learn.microsoft.com/en-us/aspnet/core/blazor/forms-and-input-components?view=aspnetcore-6.0 – Wei Chen Chen Sep 09 '22 at 08:21
  • Yes I have looked at that both while developing the concept I have now and again browsing through it in context of this – Jeppe Nielsen Sep 09 '22 at 08:53

3 Answers3

2

What I want is for all validations to run at the same time.

Not sure what you mean? All registered validations are run when you call Validate. There's has to be a sequence. If you want to change the sequence then you need to write your own validator.

the order of when validations are run seems to be based on the type

Validate on the edit context looks like this. It simply invokes any delegates registered with the OnValidationRequested event.

    public event EventHandler<ValidationRequestedEventArgs>? OnValidationRequested;

    public bool Validate()
    {
        OnValidationRequested?.Invoke(this, ValidationRequestedEventArgs.Empty);
        return !GetValidationMessages().Any();
    }

DataAnnotationsValidator or whatever validator you use registers a handler on this event.

In your case the validator is finding fields to validate by searching through the properties in the Model (referenced in EditContext) for specifc attributes. The first attribute it looks for is Required, ....

MrC aka Shaun Curtis
  • 19,075
  • 3
  • 13
  • 31
0

I'm apparently not allowed to write comments - only answers - but you're right and what Shaun Curtis says is definitely wrong. For anybody doubting this there's a gist here.

When using <DataAnnotationsValidator /> the validation attributes on the model are processed first - only once all of the attribute validation rules pass will the IValidatableObject.Validate method be called.

The functionality is by design - IValidatableObject is to validate the entire object once all of its properties have been filled out. Why is IValidatableObject.Validate only called if property validation passes?

0

I think Required validation is checked first for a reason. All subsequent validations can and must be run only after the property has a value.

Otherwise, all the other validations would fail by default. It's the framework just reminding you that you forgot to type in a field. After you supply the value, then it goes on to validate its format and what not.

I hope that makes sense.

mojorisinify
  • 377
  • 5
  • 22