this is not related to this project; but because now you've become an expert in Mudblazor - could you create a DatePicker component that receive DateOnly date and validate them via FluentValidation . Add this component in Components/shared something like that - use the base component of mudblazor as much as you can
Assistant
I'll explore how MudBlazor and FluentValidation are used in this project so we can plan a reusable DateOnly DatePicker component.
I have enough context: the project uses MudDatePicker with DateTime? and server-side FluentValidation, while the design system documents client-side MudForm + FluentValidation that isn't implemented yet. I'll plan the component and validation bridge together.
[tool: ask_question — Tool use: ask_question]
[tool: create_plan — Tool use: create_plan]
User
ok seems good; change where it could be use
Assistant
I'll find every MudDatePicker usage so we can add concrete migration targets to the plan.
Updated the plan to include migrating all 11 MudDatePicker call sites across 9 files.
What changed in the plan
Tier A — MudForm steps (7 files, 8 pickers)
Full migration: DateOnly? fields, AppDateField, plus MudForm wired with Model + Validation + For:
RegistrationCaseDetail.razor — birth date (2 duplicate blocks)
CivilStatusStep.razor — marriage date
HouseholdStep.razor — new member birth date
ResidenceStep.razor — valid from, valid until, decision date
ChangeOfAddressCaseDetail.razor — effective date
BirthDeclarationCaseDetail.razor — date of birth
Tier B — Dialogs (3 files)
Component swap only (no MudForm refactor):
NationalRegisterSearchDialog.razor
ParentLinkDialog.razor
OpenChangeOfAddressDialog.razor
Each migration removes the DateTime? ↔ DateOnly conversion boilerplate. Verification now includes confirming zero remaining MudDatePicker usages in the Web project.
Say "go ahead" or "execute the plan" when you want me to implement it.
User
ok go ahead
Assistant
Implementing the validation bridge and AppDateField, then migrating all MudDatePicker call sites.
Would it be possible to have MinDate and MaxDate as a DateOnly or a DateTime?
Assistant
Yes — we can accept both on the same MinDate/MaxDate parameters by resolving DateTime or DateOnly at runtime. Updating AppDateField and simplifying call sites.
The parameter is DateOrDateTime? — not a union, but call sites feel like one.
3. Separate optional parameters
csharp
[Parameter] public DateTime? MaxDate { get; set; }
[Parameter] public DateOnly? MaxDateOnly { get; set; }
Explicit, but two names instead of one.
4. Third-party OneOf<DateOnly, DateTime>
Used in some codebases; heavier than you need here.
Summary: No built-in union type. For AppDateField, object? is the quick option; a small DateOrDateTime struct is the typed alternative if you want compile-time clarity without object. I can refactor to that if you prefer.
User
In another codebase; I've this code
@FluentMudValidation.cs (52-63)
which is on an Abstract class which inerit from AbstractValidator - what do you think of it?
Assistant
Reviewing your project's validation bridge for comparison.
The idea is good: a base validator that validates one property via an expression fits MudBlazor well and is nicer than passing raw property name strings.
Putting it on an AbstractValidator<T> subclass is a valid design. Compared with extension methods on IValidator<T> (what we used in this repo), it gives you:
Expression-based API — ValidatePropertyAsync(model, m => m.BirthDate) lines up with Blazor For
Co-location — validation logic stays on the validator class
Reusable helper — one implementation for all feature validators
That said, the snippet as written has several problems.
1. Inverted if logic
csharp
if (propertyExpression.Body is not MemberExpression memberExpression)
{
string propertyName = propertyExpression.Member.Name; // ❌ no .Member on Expression
You want the success branch to use memberExpression.Member.Name, not the not branch. As written, when the body is a MemberExpression, the method falls through with no return.
2. Nullable value types
For DateOnly?, int?, etc., the body is often a UnaryExpression (convert), not a direct MemberExpression:
csharp
m => m.BirthDate // MemberExpression
m => m.NullableDate // often UnaryExpression wrapping MemberExpression
Handle both:
csharp
private static string GetPropertyName<T, TProp>(Expression<Func<T, TProp>> expression)
{
var body = expression.Body;
if (body is UnaryExpression { Operand: MemberExpression member })
body = member;
if (body is MemberExpression memberExpression)
return memberExpression.Member.Name;
throw new ArgumentException("Expression must be a property access.", nameof(expression));
}
User
The way we call it in this other project is the following
Validation="@((string value) => Validator.ValidateRowAsync(_model.Reason, () => _model.Reason.ReasonCode))" and we need the For also ... is that normal? Is there a better way to do that?
Assistant
The short answer: For is normal and usually required for MudForm + FluentValidation field errors. Per-field Validation on top of that is usually not — it’s a second validation path and tends to be redundant or awkward.
What each piece does
Piece
Role
MudForm.Model
The object FluentValidation validates
MudForm.Validation
(model, propertyName) => errors — runs when a field changes
For
Tells MudBlazor / EditContext which property this field is — needed to show errors on the right control
Field Validation
Separate per-field validator (often (string value) => ... on MudTextField)
If MudForm already has Model + Validation, you normally only need For on the field, not another Validation on the same field.
(string value) — Mud field-level validation passes the field value. FluentValidation usually validates the whole model + property name, so value is often ignored. That’s a sign you’re mixing two different APIs.
() => _model.Reason.ReasonCode — that’s a closure, not a property expression on Reason. For FluentValidation IncludeProperties, you want something like:
csharp
r => r.ReasonCode // Expression<Func<Reason, string>>
with model _model.Reason.
Validating _model.Reason while the form might be — validator type and form model must align, or property names won’t match what MudForm sends.
User
We are doing this way because we use the Inline editing mode cell on a mud data grid ; which we put the fluentvalidation , and wrap around a form
Assistant
That context changes things. Per-cell validation in DataGridEditMode.Cell is a different beast than a normal page form, so your approach is more understandable — but it can still be simplified.
Why a page-style MudForm pattern breaks down
On a typical form:
One Model
One Validation delegate
Many fields with For
In inline cell editing:
Only one cell is in edit mode at a time
The grid row item is the model, not a page-level DTO
There often isn’t a single “submit” moment — validation runs on blur/commit per cell
EditContext / For wiring is harder because the editor lives inside a cell template, not a classic form layout
So putting FluentValidation on the cell editor is a reasonable choice. You’re not doing it “wrong” for grids — you’re adapting to MudDataGrid’s model.
Is Validation + For normal here?
Partially.
Piece
In inline cell edit
Field Validation
Makes sense — Mud calls it when the cell value changes
For
Only needed if a parent MudForm + EditContext is driving errors. If validation is entirely on the field, Mud can show errors from Validation alone
If you have both:
MudForm around the cell and field ValidationandFor
→ you often have two pipelines doing the same job.
Pick one:
Mini MudForm per row + form Validation + For on fields
Field Validation only (no For) for cell mode
Row commit validation only (validate whole row when leaving edit mode)
For cell mode, (2) or (3) is usually simplest.
What’s still awkward in your call
Dataset releases
Not yet included in a published release.
[tool: glob_file_search — Tool use: glob_file_search • Searched codebase "" • 1 result in 1 directory]
Full solution build passes. The only remaining MudDatePicker is inside AppDateField itself.
Internally they’re object? and resolved via a small switch — DateTime is passed through, DateOnly is converted to DateTime for the underlying MudDatePicker. The bound Value stays DateOnly?.
Call sites were updated to use MaxDate="@DateTime.Today" again, so you don’t need DateOnly.FromDateTime(...) for bounds.
3. Typos / compile issues
ValidateRowAync → ValidatePropertyAsync
ValidatationContext → ValidationContext
ValidateAsync only exists on AbstractValidator<T>, not on a static helper class
4. Redundant <TModel> generic
If the class is already abstract class FluentFormValidator<T> : AbstractValidator<T>, the method should use that T, not a separate TModel:
csharp
public abstract class FluentFormValidator<T> : AbstractValidator<T>
{
public async Task<IEnumerable<string>> ValidatePropertyAsync<TProperty>(
T model,
Expression<Func<T, TProperty>> propertyExpression)
{
var propertyName = GetPropertyName(propertyExpression);
var result = await ValidateAsync(
ValidationContext<T>.CreateWithOptions(
model,
o => o.IncludeProperties(propertyName)));
return result.IsValid
? []
: result.Errors.Select(e => e.ErrorMessage);
}
public Func<object, string, Task<IEnumerable<string>>> ToMudValidateValue() =>
async (model, propertyName) =>
{
if (model is not T instance) return [];
var result = await ValidateAsync(
ValidationContext<T>.CreateWithOptions(
instance,
o => o.IncludeProperties(propertyName)));
return result.IsValid ? [] : result.Errors.Select(e => e.ErrorMessage);
};
}
MudForm still passes a string property name, so you still need ToMudValidateValue() for MudForm.Validation. The expression overload is useful for manual/field-level validation.
5. Trade-off vs extension methods
Base class approach
Extension method approach
Expression API on validator
Works with any IValidator<T>
Every validator must inherit your base
Existing AbstractValidator<T> unchanged
Natural ValidateAsync access
Needs map overload for form models
Verdict
Good direction, especially the expression-based property validation. Fix the pattern-matching bug, handle nullable unary expressions, drop the extra TModel generic, and keep a ToMudValidateValue() bridge for MudForm’s (model, propertyName) callback.
Note: the bottom of FluentMudValidation.cs in this repo looks like a partial paste of that other code (instance method on a static class, incomplete logic). That block should be removed or moved to a proper FluentFormValidator<T> base class.
If you want this refactored in the repo, switch to Agent mode and I can wire it up cleanly.
Model
_model
For + field Validation — often duplicates work: form validates via Validation, field also validates via its own delegate.
Better patterns
1. Form-level only (what we did in this repo) — preferred for FluentValidation
(string value) — FluentValidation doesn’t need the string; it validates the model property. Fine to ignore value, but it signals a mismatch between Mud’s field API and FV’s model API.
() => _model.Reason.ReasonCode — still a closure, not a property expression. Prefer:
csharp
r => r.ReasonCode
with model context.Item or row.Reason.
Nested _model.Reason — in a grid, the row item should usually be the thing the validator targets:
razor
Validator.ValidatePropertyAsync(context.Item, x => x.ReasonCode)
Better patterns for MudDataGrid + FluentValidation
Option A — Validate on row commit (often best)
Validate the whole row when the user finishes editing (row/cell commit), not on every keystroke:
csharp
// On CommitEdit / RowEditCommit
var result = await _validator.ValidateAsync(row);
if (!result.IsValid) { show errors; cancel commit; }
One FluentValidation call per commit
No per-cell Validation / For spaghetti
Fits FV’s strength (rules across multiple properties)
Option C — Cell mode: thin field wrapper (closest to what you have)
For cell edit templates, a small helper keeps Razor clean:
csharp
// On validator base class
public Task<string?> ValidateCellAsync<TProp>(
T row,
Expression<Func<T, TProp>> property)
{
var errors = await ValidatePropertyAsync(row, property);
return Task.FromResult(errors.FirstOrDefault());
}
razor
<MudTextField @bind-Value="context.Item.ReasonCode"
Validation="@(_ => Validator.ValidateCellAsync(context.Item, x => x.ReasonCode))" />
No For unless you also use MudForm
Expression is correct (x => x.ReasonCode)
Returns what Mud’s field Validation expects (string? error)
Option D — AppDateField-style for grid cells
Same idea as your AppDateField: read errors from EditContextor accept a Validation delegate — but in grids, delegate-only is often enough.
Practical recommendation for your project
For inline cell editing:
Prefer row commit validation when rules span columns (e.g. start date < end date).
Use per-cell validation only for simple single-field rules (required, max length).
Avoid For unless you wrap the row in MudForm.
Replace () => _model.Reason.ReasonCode with x => x.ReasonCode on the row model.
Don’t pass (string value) into FV — use a helper that takes (row, expression) and returns string? for Mud.
So: your motivation is valid; the pattern is heavier than it needs to be because Mud field Validation, For, and FluentValidation are layered without a single clear owner. For grids, either form-per-row or validate-on-commit + small cell helper is usually cleaner than Validation + For on every cell.
If you can share whether you use Cell or Row edit mode and a sample CellEditTemplate, I can sketch the exact markup for that case.