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

[HxInputDate] Not invoke field change #83

Closed
petrkasnal opened this issue Feb 23, 2022 · 8 comments
Closed

[HxInputDate] Not invoke field change #83

petrkasnal opened this issue Feb 23, 2022 · 8 comments
Assignees
Labels
question Further information is requested
Milestone

Comments

@petrkasnal
Copy link

Hi, i maybe found little bug in component hxinputdate. But i'm not really sure if it is a bug. When is value changed component only notification validation state changed but don't notification field changed. Normally is invoke field change too. I use it for show/hide submit button. But maybe i haven't right and this is correct behaviour.

Thank you very much. :)
image

@hakenr
Copy link
Member

hakenr commented Feb 24, 2022

Hi @petrkasnal,
the ValueChanged and editContext.NotifyFieldChanged callbacks are called from CurrentValue property setter (InputBase). These are called only when the value gets parsed correctly.
See https://github.com/dotnet/aspnetcore/blob/e0110ad9ea7e7ba646aff35832fedb5a588c6e1d/src/Components/Web/src/Forms/InputBase.cs#L72-L81

@hakenr hakenr self-assigned this Feb 24, 2022
@hakenr hakenr added the question Further information is requested label Feb 24, 2022
@petrkasnal
Copy link
Author

petrkasnal commented Feb 24, 2022

@hakenr thank you for response. I understand, but when you insert correctly date and after write something incorrect like text the NotifyFieldChange isn't invoke. And i can't update my property FormValid. I update button state on OnFieldChange, beacuse i found many tutorials on this. Or should i update FormValidate property another way? I tried update my property in event OnValidationStateChanged with method Validate But then it's called over and over again. Can you tell me how to do another way please? When i try your HxInputNumber it works just like that.

InputNumber:
ezgif-2-bd4844f661

InputDate
ezgif-2-a9086e5bd2

@hakenr
Copy link
Member

hakenr commented Feb 24, 2022

Aah, I see.
Unfortunately, you are hitting behavior we are currently trying to fix in Blazor itself.
See dotnet/aspnetcore#40097 and related PRs.

(The HxInputNumber currently has a built-in workaround/hack which helps with the behavior, but we want this to be solved properly and thus we are trying to fix Blazor InputBase itself.)

Anyway, the EditContext.OnFieldChanged is defined as

An event that is raised when a field value changes.

But the field value does not change when the input text is not valid.
I think that OnValidationStateChanged should be a better fit. You said you tried that without success. How did your code looked like?

@petrkasnal
Copy link
Author

petrkasnal commented Feb 24, 2022

Aaah ok thank you very much :) I'll look forward to it.

I try it like this. But it's logicaly again call this method to infinite. How i can do it correctly please? On google i can find only solution with field changed. Thank you
image

@hakenr hakenr added this to the Backlog milestone Feb 26, 2022
@hakenr
Copy link
Member

hakenr commented Mar 7, 2022

@jirikanda The bahavior of HxInputNumber and HxInputDate should be aligned.

@petrkasnal
Copy link
Author

@jirikanda The bahavior of HxInputNumber and HxInputDate should be aligned.

Thank you very much :)

@hakenr
Copy link
Member

hakenr commented Mar 7, 2022

Not fixed/verified yet, just a comment for my colleague. :-D

@hakenr hakenr reopened this Mar 7, 2022
@jirikanda
Copy link
Contributor

jirikanda commented Mar 15, 2022

Implementation details:

  • HxInputNumber sets value using the CurrentValueAsString property setter.
  • HxInputDate sets value using the CurrentValue property setter.

The small difference can be hidden in this line in CurrentValue setter - it means when the value is fixed (modified) to the previosly valid value, CurrentValue does not notify the change.

But when you implement the form well, I believe it works as expected.

@page "/Issue83Test"

<h1>Issue 83</h1>

<EditForm EditContext="@numberEditContext">
    <HxInputNumber @bind-Value="@numberModel.Value" Label="Number" />
    <HxButton Text="Submit" Enabled="@(!numberEditContext.GetValidationMessages().Any())" Color="ThemeColor.Primary" />
</EditForm>

<EditForm EditContext="@dateEditContext">
    <HxInputDate @bind-Value="@dateModel.Value" Label="Date" />
    <HxButton Text="Submit" Enabled="@(!dateEditContext.GetValidationMessages().Any())" Color="ThemeColor.Primary" />
</EditForm>

@code
{
    private EditContext numberEditContext;
    private GenericModel<int> numberModel;

    private EditContext dateEditContext;
    private GenericModel<DateTime> dateModel;

    private void HandleValidationStateChanged(object sender, ValidationStateChangedEventArgs args)
    {
        StateHasChanged();
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();

        numberModel = new GenericModel<int>();
        numberEditContext = new EditContext(numberModel);
        numberEditContext.OnValidationStateChanged += HandleValidationStateChanged;

        dateModel = new GenericModel<DateTime>();
        dateEditContext = new EditContext(numberModel);
        dateEditContext.OnValidationStateChanged += HandleValidationStateChanged;
    }

    private class GenericModel<T>
    {
        public T Value { get; set; }
    }
}

hakenr pushed a commit that referenced this issue Mar 17, 2022
…ůči NotifyFileChanged/ValidationStateChanged (github #83 [HxInputDate] Not invoke field change)
@hakenr hakenr closed this as completed Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants