Blazorise Fire EditContext.OnFieldChanged C#

Razor code

<EditForm Model="person" Context="editContext">
    @{
        editContext.OnFieldChanged += (sender, eventArgs) =>
        {
            Console.WriteLine("OnFieldChanged for FieldName = " + eventArgs.FieldIdentifier.FieldName);
        };
    }

    <Field>
        <NumericEdit @bind-Value="person.Age"></NumericEdit>
    </Field>

    <Column ColumnSize="ColumnSize.Is2">
        <InputNumber @bind-Value="person.Age" />
    </Column>

    <button type="submit">Save</button>
</EditForm>

Problem

The OnFieldChanged-event is not fired when NumericEdit is changed. But it's fired when InputNumber is changed.

Asked Oct 07 '21 06:10
avatar StefH
StefH

6 Answer:

Blazorise does not use EditForm so it cannot fire the OnFieldChanged event. Internally it only uses EditContext, but only partially to make the data annotations work with validation. I don't have any plan to support EditForm at this moment.

1
Answered Jun 19 '20 at 08:00
avatar  of stsrki
stsrki

Hello @stsrki,

Is there any other clever way of making the OnFieldChanged work for any Blazorise edit component?

I got this workaround: Razor

<NumericEdit TValue="int" Value="person.Age" ValueChanged="@((v) => ValueChanged(v, editContext))" ></NumericEdit>

````
&
c#
``` c#
void ValueChanged(int value, EditContext context)
    {
        Console.WriteLine("OnFieldChanged !! " + value);

        person.Age = value;

        context.NotifyFieldChanged(new FieldIdentifier(person, "Age"));
    }

But this is just a work-acound to use ValueChanged to notify the context that a field has been changed (e.g. so I can execute my custom valiation)

1
Answered Jun 25 '20 at 19:57
avatar  of StefH
StefH

That seems like a valid approach, if you want to use EditForm.

Is there any reason why you dont use Blazorise Validations component?

1
Answered Jun 25 '20 at 20:02
avatar  of stsrki
stsrki
  1. It's valid code, however I don't want to add that ValueChanged to all my input fields, it's very verbose

  2. Using a EditForm is not mandatory, however this is a component which defines the EditContext I believe? And I want to use that EditContext.

  3. This question is related to : https://github.com/stsrki/Blazorise/issues/995 I want to use one of these packages to use automatic FluentValidation, so not coding all Validations manually.

<PackageReference Include="Blazored.FluentValidation" Version="1.3.0" />
<PackageReference Include="Accelist.FluentValidation.Blazor" Version="2.1.0" />
1
Answered Jun 25 '20 at 20:37
avatar  of StefH
StefH

To tell you the truth I don't see much value from using FluentValidation instead of built-in support for validators or data annotations. But, I understand if that is something people wish to use.

Now back to our problem here. From what I can see the biggest issue is that EditContext if not rightly shared between components. Internally it is used, and it calls EditContext.NotifyFieldChanged( fieldIdentifier ); but I will need to figure why it's not working with EditForm.

1
Answered Jun 25 '20 at 21:02
avatar  of stsrki
stsrki

I did try something: extending the NumericEdit 👍 w ``` c# public class NumericEdit2 : NumericEdit { [CascadingParameter] EditContext CascadedEditContext { get; set; }

    protected FieldIdentifier FieldIdentifier { get; set; }

    protected override void OnInitialized()
    {
        FieldIdentifier = FieldIdentifier.Create(ValueExpression);

        base.OnInitialized();
    }

    protected async override Task OnInternalValueChanged(TValue value)
    {
        await base.OnInternalValueChanged(value);

        if (CascadedEditContext != null)
        {
            CascadedEditContext.NotifyFieldChanged(FieldIdentifier);
        }
    }
}

```

with this code change, the EditContext (if it's there) will be notified.

maybe you can add this logic to your base-class ?

1
Answered Jun 26 '20 at 18:14
avatar  of StefH
StefH