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.
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.
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)
That seems like a valid approach, if you want to use EditForm.
Is there any reason why you dont use Blazorise Validations
component?
It's valid code, however I don't want to add that
ValueChanged
to all my input fields, it's very verboseUsing a EditForm is not mandatory, however this is a component which defines the
EditContext
I believe? And I want to use that EditContext.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" />
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.
I did try something: extending the NumericEdit 👍
w
``` c#
public class NumericEdit2
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 ?
Read next
- `gradle-test-kit` depends on `org.codehaus.groovy:groovy-all:jar`, but groovy-all has `<packaging>pom</packaging>` - Groovy gradle
- Data corruption with OpenMPI 4.0.4 and UCX 1.9.0 - C ompi
- factory_girl_rails/factory_bot_rails RuntimeError: can't modify frozen Array - Gherkin factory_bot_rails
- OnlyFans Trying to run just doesn't work Python
- Error loading - UnityExplorer
- Microsoft/VSCode - Cannot create a Python Interactive Window - vscode-jupyter
- Missing settings in the GUI - Solaar
- appium The capabilities UnlockType & UnlockKeys doesn't work with the mobile device ( brand Oppo ) - JavaScript