Skip to content

Commit 40eb46c

Browse files
committed
Fixed EditForm with edit context validation not working properly
1 parent 07d2b92 commit 40eb46c

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/DotNetElements.AppFramework.MudBlazorExtensions/Components/MudEditFormDialog.razor

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
<MudDialogEx>
77
<DialogContent>
8-
@if (Value is not null && editContext is not null)
8+
@if (Value is not null)
99
{
10-
<EditForm EditContext="@editContext">
10+
<EditForm @ref="editFormRef" Model="@Value">
1111
<DataAnnotationsValidator />
1212
@FormContent(Value)
1313
</EditForm>
@@ -27,7 +27,7 @@
2727
@CustomDialogActions
2828
}
2929
<MudButton OnClick="OnCancelAsync_Internal" Variant="Variant.Outlined" Class="ml-4">@CancelButtonText</MudButton>
30-
@if (Value is not null && editContext is not null)
30+
@if (Value is not null)
3131
{
3232
<MudButton Color="Color.Primary" OnClick="OnSubmitAsync_Internal" Variant="Variant.Filled" Disabled="@SubmitButtonDisabled" Class="ml-4">@SubmitButtonText</MudButton>
3333
}
@@ -66,16 +66,17 @@
6666
[Parameter]
6767
public string CancelButtonText { get; set; } = "Cancel";
6868

69-
private EditContext? editContext;
69+
private EditForm? editFormRef;
7070

7171
private bool hasError;
7272
private string? errorMessage;
7373

7474
public bool Validate()
7575
{
76-
ArgumentNullException.ThrowIfNull(editContext);
76+
ArgumentNullException.ThrowIfNull(editFormRef);
77+
ArgumentNullException.ThrowIfNull(editFormRef.EditContext);
7778

78-
return editContext.Validate();
79+
return editFormRef.EditContext.Validate();
7980
}
8081

8182
protected override async Task OnInitializedAsync()
@@ -93,21 +94,13 @@
9394
errorMessage = args.ErrorMessage;
9495
}
9596

96-
// todo check how this behaves when a parameter is updated!!!
97-
protected override void OnParametersSet()
98-
{
99-
if (Value is null)
100-
return;
101-
102-
editContext = new EditContext(Value);
103-
}
104-
10597
private async Task OnSubmitAsync_Internal()
10698
{
10799
ArgumentNullException.ThrowIfNull(Value);
108-
ArgumentNullException.ThrowIfNull(editContext);
100+
ArgumentNullException.ThrowIfNull(editFormRef);
101+
ArgumentNullException.ThrowIfNull(editFormRef.EditContext);
109102

110-
EditFormSubmitArgs<TValue, TReturnValue> args = new(Value, editContext);
103+
EditFormSubmitArgs<TValue, TReturnValue> args = new(Value, editFormRef.EditContext);
111104

112105
if (OnSubmit.HasDelegate)
113106
await OnSubmit.InvokeAsync(args);

0 commit comments

Comments
 (0)