Skip to content

Commit

Permalink
fix(module: form): validation would be reset when model changes (#1035)
Browse files Browse the repository at this point in the history
* fix(module:form): form.reset() will make new editContext

fixes 947

* fix(module:form): validation is reset when model changes

fixes 851
fixes 982
fixes 991

* feat(module:form): validation reset method
  • Loading branch information
anddrzejb committed Jan 24, 2021
1 parent f91f9c4 commit 318317c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions components/form/Form.razor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AntDesign.Forms;
using AntDesign.Internal;
Expand Down Expand Up @@ -61,7 +60,18 @@ public partial class Form<TModel> : AntDomComponentBase, IForm
public string Name { get; set; }

[Parameter]
public TModel Model { get; set; }
public TModel Model
{
get { return _model; }
set
{
if (!(_model?.Equals(value) ?? false))
{
_model = value;
_editContext = new EditContext(Model);
}
}
}

[Parameter]
public bool Loading { get; set; }
Expand Down Expand Up @@ -95,6 +105,7 @@ public partial class Form<TModel> : AntDomComponentBase, IForm
private EditContext _editContext;
private IList<IFormItem> _formItems = new List<IFormItem>();
private IList<IControlValueAccessor> _controls = new List<IControlValueAccessor>();
private TModel _model;

ColLayoutParam IForm.WrapperCol => WrapperCol;

Expand Down Expand Up @@ -153,6 +164,7 @@ private async Task OnInvalidSubmit(EditContext editContext)
public void Reset()
{
_controls.ForEach(item => item.Reset());
_editContext = new EditContext(Model);
}

void IForm.AddFormItem(IFormItem formItem)
Expand Down Expand Up @@ -188,5 +200,10 @@ public bool Validate()
{
return _editContext.Validate();
}

public void ValidationReset()
{
_editContext = new EditContext(Model);
}
}
}

0 comments on commit 318317c

Please sign in to comment.