MudForm: Base IsValid on required fields having a value, not being touched#13423
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
…populated form (MudBlazor#5196) validity on load
|
Reproduced the actual consumer snippets in a live browser harness:
Two behaviours worth noting so they aren't misread as new bugs:
|
…t (review) Avoids the Cast<object?>().Any() enumerator allocation for the common ICollection case; the foreach fallback disposes the enumerator for other IEnumerable types.
… a fast-path The ICollection/foreach form added a coverage gap and a Sonar S1751 'loop only runs once' smell for a negligible gain on a branch MudSelect already overrides. Cast().Any() is a single covered statement.
Exercises the => Touched fallback that external implementers without a HasValue override rely on.
MudForm decided validity from
requiredAllTouched, usingTouchedas a proxy for "required field satisfied".Fix: base validity on whether each required field holds a value.
IFormComponent.HasValue()— a default interface method (returnsTouched, so existing external implementers are unaffected), overridden on MudFormComponent; empty strings and empty collections count as no value.noErrors && required.All(x => x.HasValue())in bothOnEvaluateFormand first-render init.Behavior notes for upgraders:
MudRadioGroupis now correctly invalid.Validationdelegate reads as valid until it is validated (the delegate runs on interaction), and a debounced field with an uncommitted value is treated as having no value until it commits.Related issues
Touchedon parameter-driven value sets, so a pre-populated required field becameHasValue=true butTouched=false — and the form read invalid on load (regressed from 9.5.0).falsewith Required Fields #5196.IsValidbinding when required fields are empty on initial render #12664.