Skip to content

Form validation for Blazor (EditForm) to include validation error message for data annotations and custom validation at the same time #66966

@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


Can we please update the Blazor validation (EditForm) to include validation error message for both data annotations and custom validation at the same time. Currently the data annotation validation errors appear initially and when these are resolved, the custom validation errors show. It would be more user friendly for all validations to appear at the same time the editContext.Validate() is called. Here is an example form:

<EditForm EditContext="@editContext" OnSubmit="@HandleSubmit">
    <DataAnnotationsValidator />
    <div class="section">

<div class="row pb-3">
            <div class="offset-lg-3 col-lg-1">
                <label class="col-form-label">Code</label>
            </div>
            <div class="col-lg-4">
                <InputText class="form-control" @bind-Value="@TypeToAdd.Code" maxlength="15" />
                <ValidationMessage For="@(() => TypeToAdd.Code)" />
            </div>
        </div>
        <div class="row pb-3">
            <div class="offset-lg-3 col-lg-1">
                <label class="col-form-label">Name</label>
            </div>
            <div class="col-lg-4">
                <InputText class="form-control" @bind-Value="@TypeToAdd.Name" maxlength="100" />
                <ValidationMessage For="@(() => TypeToAdd.Name)" />
            </div>
        </div>
    </div>
    <button type="submit" autofocus class="button button-green"><span class="base-icon base-icon--save" style="background-color: white;"></span>Save</button>

</EditForm>

with the Model:

public class TypeModel : IValidatableObject
{
    public int TypeId { get; set; }
    [Required]
    public string Code { get; set; } = string. Empty;
    public string Name { get; set; } = string. Empty;
    public bool Active { get; set; }
    public DateTime ModifiedDateTime { get; set; }
    public bool IsPendingSync { get; set; }

public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext context)
    {
        var results = new List&lt;ValidationResult&gt;();

if (Name == null || Name == string. Empty)
        {
            results. Add(new ValidationResult("Name is required.", new[] { nameof(Name) }));
        }

return results;
    }
}
  

Here is the 'Handle Submit' method:

private async Task HandleSubmit()
{
    var isValid = editContext.Validate(); // 🔥 THIS is the key

if (!isValid)
        return;

await SaveUpdate();
}

If save is selected without entering any data into the InputText fields, the ‘Code’ field shows an error message but not ‘Name’. Once ‘Code’ has a value, then the ‘Name’ shows an error message.

I’m using .Net 10 and the latest Visual Studio 2026 Insiders [11709.129].

Thanks for considering.


Original Comments

Feedback Bot on 4/21/2026, 00:57 AM:

Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. You will hear from us in about a week on our next steps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.area-blazorIncludes: Blazor, Razor Components
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions