Blazor – 手动设置表单数据时清除验证错误的问题

huangapple go评论41阅读模式
英文:

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">

huangapple
  • 本文由 发表于 2023年5月10日 23:09:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定