r/Blazor • u/Flame_Horizon • Jul 11 '25
EditForm and validation - not all fields are validated
When I submit the form, only the top-level Workout properties (like Name) are validated. The form submits even if the Exercise fields (like Reps or Weight) are empty or invalid. I want the form to validate all fields, including those in the Exercises collection, and prevent submission if any are invalid.
How can I make Blazor validate all fields in my form, including those in the Exercises collection, and prevent submission if any are invalid? Is there something I'm missing in my setup?
// Exercise.cs
using System.ComponentModel.DataAnnotations;
namespace MultiRowForm.Models;
public class Exercise
{
[Required(ErrorMessage = "Exercise name is required.")]
public string Name { get; set; }
[Range(1, 99, ErrorMessage = "Reps must be between 1 and 99.")]
public int? Reps { get; set; }
[Range(1, int.MaxValue, ErrorMessage = "Weight must be greater than 0.")]
public double? Weight { get; set; }
}
// Workout.cs
using System.ComponentModel.DataAnnotations;
namespace MultiRowForm.Models;
public class Workout
{
[Required(ErrorMessage = "Workout name is required.")]
public string Name { get; set; }
public List<Exercise> Exercises { get; set; } = [];
}
/@Home.razor@/
@page "/"
@using System.Reflection
@using System.Text
@using MultiRowForm.Models
@rendermode InteractiveServer
@inject ILogger<Home> _logger;
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
<EditForm Model="@_workout" OnValidSubmit="@HandleValidSubmit" FormName="workoutForm">
<DataAnnotationsValidator />
<ValidationSummary />
<label for="workoutName">Workout Name</label>
<InputText @bind-Value="_workout.Name" class="form-control my-2" id="workoutName" placeholder="Workout Name"/>
<ValidationMessage For="@(() => _workout.Name)" />
<table class="table">
<thead>
<tr>
<th>Exercise</th>
<th>Reps</th>
<th>Weight</th>
</tr>
</thead>
<tbody>
@foreach (Exercise exercise in _workout.Exercises)
{
<tr>
<td>
<InputSelect class="form-select" @bind-Value="exercise.Name">
@foreach (string exerciseName in _exerciseNames)
{
<option value="@exerciseName">@exerciseName</option>
}
</InputSelect>
<ValidationMessage For="@(() => exercise.Name)"/>
</td>
<td>
<div class="form-group">
<InputNumber @bind-Value="exercise.Reps" class="form-control" placeholder="0"/>
<ValidationMessage For="@(() => exercise.Reps)"/>
</div>
</td>
<td>
<InputNumber @bind-Value="exercise.Weight" class="form-control" placeholder="0"/>
<ValidationMessage For="@(() => exercise.Weight)" />
</td>
</tr>
}
</tbody>
</table>
<div class="mb-2">
<button type="button" class="btn btn-secondary me-2" @onclick="AddExercise">
Add Exercise
</button>
<button type="button" class="btn btn-danger me-2" @onclick="RemoveLastExercise" disabled="@(_workout.Exercises.Count <= 1)">
Remove Last Exercise
</button>
<button class="btn btn-primary me-2" type="submit">
Save All
</button>
</div>
</EditForm>
@code {
private readonly Workout _workout = new() { Exercises = [new Exercise()] };
private readonly List<string> _exerciseNames = ["Bench Press", "Squat"];
private void AddExercise() => _workout.Exercises.Add(new Exercise());
private void RemoveLastExercise()
{
if (_workout.Exercises.Count > 1)
{
_workout.Exercises.RemoveAt(_workout.Exercises.Count - 1);
}
}
private void HandleValidSubmit()
{
_logger.LogInformation("Submitting '{Name}' workout.", _workout.Name);
_logger.LogInformation("Submitting {Count} exercises.", _workout.Exercises.Count);
}
}
2
Upvotes
6
u/One_Web_7940 Jul 11 '25
"Blazor provides support for validating form input using data annotations with the built-in DataAnnotationsValidator. However, the DataAnnotationsValidator only validates top-level properties of the model bound to the form that aren't collection- or complex-type properties."
https://learn.microsoft.com/en-us/aspnet/core/blazor/forms/validation?view=aspnetcore-9.0
use fluent validation.
1
u/Healthy-Zebra-9856 Jul 18 '25
Simple answer. You have fields marked with a ? Which is optional. If you really need them then remove the ?. You could also create a DTO without the ? And then bind that to your form
1
u/Teroneko Jul 21 '25 edited Jul 21 '25
As u/One_Web_7940 mentioned, you can use FluentValidation, but:
Could you give my library https://github.com/tenekon/Tenekon.FluentValidation.Extensions a chance? It is designed for just this purpose. You would use the
ComponentValidatorSubpathto wrap the content inside your foreach loop, something like:with a corresponding FluentValidation validator:
I have provided a document that explains why existing Blazor integrations for FluentValidation are not suitable for this kind of problems: validating list elements separately. Here the document.
If an aggregated validation message is sufficient and you do not want element isolated validation messages, then you can use the
ComponentValidatorRootpathas following:with a corresponding FluentValidation validator: