Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
hakenr opened this issue Oct 14, 2024 · 0 comments
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

Comments

@hakenr
Copy link
Contributor

hakenr commented Oct 14, 2024

Is there an existing issue for this?

  • 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).

// Since we're not writing to CurrentValue, we'll need to notify about modification from here
EditContext.NotifyFieldChanged(FieldIdentifier);

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:

<EditForm Model="formModel">
    <DataAnnotationsValidator />
    <InputNumber @bind-Value="formModel.Value" />

    <button type="submit">Submit</button>

    <ValidationSummary />
</EditForm>

@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

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

// Since we're not writing to CurrentValue, we'll need to notify about modification from here
EditContext.NotifyFieldChanged(FieldIdentifier);

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).

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Oct 14, 2024
@mkArtakMSFT mkArtakMSFT added investigate feature-blazor-form-validation This issue is related to forms validation in Blazor labels Oct 14, 2024
@mkArtakMSFT mkArtakMSFT added this to the .NET 10 Planning milestone Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-form-validation This issue is related to forms validation in Blazor investigate
Projects
None yet
Development

No branches or pull requests

2 participants