[Blazor] InputBase - failed parsing (eg. InputNumber) triggers field validation #58407
Open
1 task done
Labels
area-blazor
Includes: Blazor, Razor Components
feature-blazor-form-validation
This issue is related to forms validation in Blazor
investigate
Milestone
Is there an existing issue for this?
Describe the bug
If the parsing of input value fails (
TryParseValueFromString()
returnsfalse
),InputBase
triggersEditContext.NotifyFieldChanged(FieldIdentifier);
which causes all the other field validations to run although the Value of the field did not change (CurrentValue
remains unchanged,ValueChanged
is not invoked,Model
is not changed).aspnetcore/src/Components/Web/src/Forms/InputBase.cs
Lines 142 to 143 in 049814c
Expected Behavior
EditContext.NotifyFieldChanged()
should be called only when theValue
of the field changes (causing revalidation of the field).Steps To Reproduce
Simple repro:
input
and type1e+10
as an unparseable input. Hit Tab to triggeronchange
for theinput
.ValidationSummary
displays both theParsingErrorMessage
and the regular validation message from theRequiredAttribute
(sinceformModel.Value
remainsnull
when the input can't be parsed):BTW: In contrast, if you reset the page:
input
, e.g.,5
. Triggeronchange
by leaving theinput
.input
again, replace the5
with1e+10
, and leave theinput
.ValidationSummary
only displays theParsingErrorMessage
, as model validation is satisfied (formModel.Value
is still5
).aspnetcore/src/Components/Web/src/Forms/InputBase.cs
Lines 142 to 143 in 049814c
I think
EditContext.NotifyFieldChanged()
should not be called when the field's value hasn't changed (which is the case if parsing fails). I'm not sure why theEditContext.NotifyFieldChanged()
call is there (possibly from the initial implementation of input components by @SteveSandersonMS), but if the intent was to ensureParsingErrorMessage
gets displayed, it should be managed by the subsequentEditContext?.NotifyValidationStateChanged();
call.Exceptions (if any)
No response
.NET Version
9.0.100-rc.1.24452.12 (same on any earlier)
Anything else?
If we agree this needs to be changed, I can prepare a PR to fix it (including new E2E tests).
The text was updated successfully, but these errors were encountered: