Skip to content

Server validation with a validator component upgrade#37113

Open
guardrex wants to merge 11 commits into
mainfrom
guardrex/server-validation-example-upgrade
Open

Server validation with a validator component upgrade#37113
guardrex wants to merge 11 commits into
mainfrom
guardrex/server-validation-example-upgrade

Conversation

@guardrex
Copy link
Copy Markdown
Collaborator

@guardrex guardrex commented May 6, 2026

Fixes #36051

Ondřej ... Per your suggestion in #35972, the general idea is to replace the MVC controller with a Minimal API validation, including the unified validation experience.

To simplify the section, the content is versioned into >=10.0, >=8.0/<10.0, and <8.0 blocks. I think we only need to focus on the new guidance in the >=10.0 coverage at the top of the section. I haven't received any complaints (or death threats 💀😨😆) from our readers on our MVC controller-based guidance.


Internal previews

📄 File 🔗 Preview link
aspnetcore/blazor/forms/validation.md aspnetcore/blazor/forms/validation

@guardrex guardrex self-assigned this May 6, 2026
@guardrex guardrex marked this pull request as ready for review May 7, 2026 13:12
@guardrex guardrex requested a review from Copilot May 7, 2026 13:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the “Server validation with a validator component” guidance in validation.md to introduce a .NET 10+ flow that uses a Minimal API-based validation endpoint and splits the content into versioned blocks.

Changes:

  • Adds a new >= aspnetcore-10.0 section demonstrating server validation via a Minimal API and a client/server validator abstraction.
  • Reorganizes the existing guidance into >=8.0/<10.0 and <8.0 moniker blocks.
  • Introduces new sample code and updated narrative around client + server validation responsibilities.

Comment thread aspnetcore/blazor/forms/validation.md
Comment thread aspnetcore/blazor/forms/validation.md Outdated
Comment thread aspnetcore/blazor/forms/validation.md Outdated
Comment thread aspnetcore/blazor/forms/validation.md Outdated
Comment thread aspnetcore/blazor/forms/validation.md Outdated
Comment thread aspnetcore/blazor/forms/validation.md Outdated
Comment thread aspnetcore/blazor/forms/validation.md Outdated
@guardrex guardrex requested a review from oroztocil May 8, 2026 18:41
@oroztocil
Copy link
Copy Markdown
Member

Hi @guardrex, sorry for not getting to this sooner. I did read through the whole PR but a first general comment is that by using a validated Minimal API endpoint (instead of a MVC controller) I meant using the built-in validation feature we added in .NET 10 that you enable simply by calling builder.Services.AddValidation(). A built-in filter in the request pipeline then validates all the DataAnnotations attributes on the request parameters automatically (including nested properties of model classes).

That should significantly simplify the examples because you don't need to build and register any custom validators (the IFormValidator types in your example), you don't need to create ProblemDetails manually, etc.

@guardrex

This comment was marked as resolved.

@guardrex

This comment was marked as outdated.

@guardrex

This comment was marked as outdated.

@guardrex
Copy link
Copy Markdown
Collaborator Author

guardrex commented May 20, 2026

@oroztocil ... I confirmed that the server-side built-in validation (AddValidation) is working with the model.

When I removed the "Accommodation" field range (1-100000) DA validation from the BWA's StarshipModel class and placed a StarshipModel class in the backend Minimal API app with an "Accommodation" field range of 200000-1000000, it sent back a 400 with a problem details that was successfully used by the custom validation component to set the error on the form correctly ...

image

If I then correct that to get the built-in validation to pass but break the special check performed in code, it then also throws the 400 with the problem details showing the expected form result ...

image

AFAICT, what's on the PR is fine with perhaps a few more code improvements.

I also perform a quick test to confirm that a custom validation attribute in the Minimal API works well with the built-in validation. I'm sure it will work fine. Then, I'll add a quick remark (or a short section) to this PR on it.

Stand-by a bit longer ... making progress here! 🎉 ... and we might have some decent draft coverage to look at soon.

@guardrex
Copy link
Copy Markdown
Collaborator Author

It works! 🎉

In the Minimal API ...

using System.ComponentModel.DataAnnotations;

namespace MinimalApiJwt;

public class CustomValidator : ValidationAttribute
{
    protected override ValidationResult IsValid(object? value,
        ValidationContext validationContext)
    {
        if (value is not null)
        {
            var intValue = (int)value;

            if (intValue > 500000)
            {
                return ValidationResult.Success!;
            }
        }

        return new ValidationResult("Accommodation must be greater than 500000.",
            [validationContext.MemberName!]);
    }
}

In the StarshipModel class in the Minimal API ...

[CustomValidator]
public int MaximumAccommodation { get; set; }

Result when an invalid figure is provided ...

image

Correcting the figure, the validation passes ...

image

@guardrex
Copy link
Copy Markdown
Collaborator Author

@oroztocil ... Made a final round of updates to the draft content. I added some remarks (not a section) on custom validation attributes. It's ready for review now.

@guardrex guardrex requested a review from Copilot May 20, 2026 13:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.

Comments suppressed due to low confidence (1)

aspnetcore/blazor/forms/validation.md:680

  • This snippet includes using Microsoft.AspNetCore.Mvc; but doesn't use MVC types. In a .Client project, that using can be misleading and may not compile unless MVC packages are referenced. Remove the unused using.
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Mvc;

namespace BlazorSample.Client;

Comment thread aspnetcore/blazor/forms/validation.md
Comment thread aspnetcore/blazor/forms/validation.md Outdated
Comment thread aspnetcore/blazor/forms/validation.md
Comment thread aspnetcore/blazor/forms/validation.md Outdated
Comment thread aspnetcore/blazor/forms/validation.md Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server validation with a validator component upgrade

3 participants