英文:
Blazor - issue clearing validation errors when manually setting form data
问题
以下是您提供的内容的翻译:
我在这里找到了很多清除代码中验证错误的示例,但似乎对我没有用。我正在使用MudBlazor控件,尽管我不认为这很重要。这是我表单的简化版本:
<EditForm Model="@Claimant"
              OnValidSubmit="@AddClaim">
        <DataAnnotationsValidator />
     <MudTextField @bind-Value="@Claimant.AddressLine1" id="AddressLine1"
                                  Label="Address Line 1" Variant="Variant.Outlined"
                                  Margin="Margin.Dense" For="@(() => Claimant.AddressLine1)" MaxLength="100"></MudTextField>
                    <MudTextField @bind-Value="@Claimant.AddressLine2"
                                  Label="Address Line 2" Variant="Variant.Outlined"
                                  Margin="Margin.Dense" For="@(() => Claimant.AddressLine2)" MaxLength="100"></MudTextField>
                <MudButton Variant="Variant.Filled" Disabled=@IsDisabled
                           Style="display: inline-flex; justify-content: space-around; align-items: flex-start;"
                           StartIcon="@Icons.Material.Filled.NoteAdd"
                           Color="Color.Primary"
                           ButtonType="ButtonType.Submit">
                    提交索赔
                </MudButton>
当我点击提交按钮而没有输入任何值时,如预期地显示错误。我有一些用于地址验证的代码,然后用户从中选择,我手动设置了一些表单字段:
protected Claimant Claimant { get; set; } = new();
private EditContext EC { get; set; }
protected override async Task OnInitializedAsync()
{
    EC = new EditContext(Claimant);
    await base.OnInitializedAsync();
}
protected void SuggestedAddressClick()
{
    var selectedAddress = SuggestedAddresses[(int)selectedValue];
    if (selectedAddress != null)
    {
        Claimant.AddressLine1 = selectedAddress.Line1;  // 设置后以下字段的错误应该被清除。
        Claimant.City = selectedAddress.City;
        Claimant.State = selectedAddress.StateProvince;
        Claimant.ZipCode = selectedAddress.PostalCode;
        EC.Validate();                                    //不起作用
        EC.NotifyValidationStateChanged();                //不起作用
        EC.NotifyFieldChanged(EC.Field("AddressLine1"));  //不起作用
        StateHasChanged();
    }
}
这段代码按预期设置了表单中的值,并且我在模型中看到了这些值。问题是,清除验证错误的代码行都不起作用。只有当我再次点击提交按钮或点击每个字段并点击或切出时,错误才会清除。
不确定我漏掉了什么。
英文:
I have found many examples here of clearing validation errors in code but
nothing seems to be working for me.  I am using MudBlazor controls although I don't think that matters.  Here is a shortened version of my form:
 <EditForm Model="@Claimant"
              OnValidSubmit="@AddClaim">
        <DataAnnotationsValidator />
     <MudTextField @bind-Value="@Claimant.AddressLine1" id="AddressLine1"
                                  Label="Address Line 1" Variant="Variant.Outlined"
                                  Margin="Margin.Dense" For="@(() => Claimant.AddressLine1)" MaxLength="100"></MudTextField>
                    <MudTextField @bind-Value="@Claimant.AddressLine2"
                                  Label="Address Line 2" Variant="Variant.Outlined"
                                  Margin="Margin.Dense" For="@(() => Claimant.AddressLine2)" MaxLength="100"></MudTextField>
                <MudButton Variant="Variant.Filled" Disabled=@IsDisabled
                           Style="display: inline-flex; justify-content: space-around; align-items: flex-start;"
                           StartIcon="@Icons.Material.Filled.NoteAdd"
                           Color="Color.Primary"
                           ButtonType="ButtonType.Submit">
                    Submit Claim
                </MudButton>
When I click the submit button with no values entered it shows the errors as expected. I have some code for address validation that the user then selects from and I set some form fields manually:
    protected Claimant Claimant { get; set; } = new();
    private EditContext EC { get; set; }
    protected override async Task OnInitializedAsync()
    {
        EC = new EditContext(Claimant);
        await base.OnInitializedAsync();
    }
     protected void SuggestedAddressClick()
    {
        var selectedAddress = SuggestedAddresses[(int)selectedValue];
        if (selectedAddress != null)
        {
            Claimant.AddressLine1 = selectedAddress.Line1;  // The error for the following fields should be cleard after setting.
            Claimant.City = selectedAddress.City;
            Claimant.State = selectedAddress.StateProvince;
            Claimant.ZipCode = selectedAddress.PostalCode;
            EC.Validate();                                    //Does Not Work
            EC.NotifyValidationStateChanged();                //Does Not Work
            EC.NotifyFieldChanged(EC.Field("AddressLine1"));  //Does Not Work
            StateHasChanged();
        }
    }
This code sets the values in form as expected and I see the values in the model. The problem is, none of the lines of code to clear the validation errors work. The errors only clear if I click the submit button again OR I click into each field and click or tab out.
Not sure what I am missing.
答案1
得分: 1
"你似乎在代码中创建了一个新的 EditContext - 但你配置 EditForm 的方式意味着它会创建自己的 EditContext。
尝试将你的 EditContext 传递给 EditForm(而不是模型)。
<EditForm EditContext="@EC" OnValidSubmit="@AddClaim">"
英文:
You appear to be creating a new EditContext in code - but the way you configured your EditForm means that will create its own EditContext.
Try passing your EditContext to the EditForm (instead of the model)
<EditForm EditContext="@EC" OnValidSubmit="@AddClaim">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论