dotnet / dotnet/aspnetcore

[Blazor] InputBase - failed parsing (eg. InputNumber) triggers field validation

Open
#58,407 1 comment 0 reactions 0 assignees View on GitHub
area-blazor bug copilot-candidate feature-blazor-form-validation
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

If the parsing of input value fails (`TryParseValueFromString()` returns `false`), `InputBase` triggers `EditContext.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).

https://github.com/dotnet/aspnetcore/blob/049814ca468cad1ea1e29412e0aa3eea182a63c1/src/Components/Web/src/Forms/InputBase.cs#L142-L143

### Expected Behavior

`EditContext.NotifyFieldChanged()` should be called only when the `Value` of the field changes (causing revalidation of the field).

### Steps To Reproduce

Simple repro:

```razor


Submit

@code {
private FormModel formModel = new FormModel();

public class FormModel
{
[Required]
public int? Value { get; set; }
}
}
```

1. Focus on the empty `input` and type `1e+10` as an unparseable input. Hit Tab to trigger `onchange` for the `input`.
2. The `ValidationSummary` displays both the `ParsingErrorMessage` and the regular validation message from the `RequiredAttribute` (since `formModel.Value` remains `null` when the input can't be parsed):
![Image](https://github.com/user-attachments/assets/6b2a9b05-9aa5-4aab-88f3-1e6b40751c6c)

BTW: In contrast, if you reset the page:
1. Enter any valid number into the `input`, e.g., `5`. Trigger `onchange` by leaving the `input`.
2. Focus on the `input` again, replace the `5` with `1e+10`, and leave the `input`.
3. The `ValidationSummary` only displays the `ParsingErrorMessage`, as model validation is satisfied (`formModel.Value` is still `5`).
![Image](https://github.com/user-attachments/assets/d48ad7e6-6920-4b31-978c-1f9b47aa2ca1)

https://github.com/dotnet/aspnetcore/blob/049814ca468cad1ea1e29412e0aa3eea182a63c1/src/Components/Web/src/Forms/InputBase.cs#L142-L143
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 the `EditContext.NotifyFieldChanged()` call is there (possibly from the initial implementation of input components by @SteveSandersonMS), but if the intent was to ensure `ParsingErrorMessage` gets displayed, it should be managed by the subsequent `EditContext?.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).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.