首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提交表单时的Blazor EditForm自定义验证消息

提交表单时的Blazor EditForm自定义验证消息
EN

Stack Overflow用户
提问于 2021-10-26 13:58:28
回答 1查看 1.5K关注 0票数 0

我们可以在Blazor中向EditForm添加自定义验证消息吗?我的表单如下所示,在提交表单时,我必须执行一些业务逻辑检查,以查看为参数提供的值是否确定,如果不是确定,则必须显示自定义动态验证消息。

代码语言:javascript
复制
 <EditForm Model="@starship" OnValidSubmit="@HandleValidSubmit">
        <DataAnnotationsValidator />
        <ValidationSummary /> 
        <p>
            <label>
                Identifier:
                <InputText @bind-Value="starship.Identifier" />
            </label>
        </p>
        <p>
            <label>
                Description (optional):
                <InputTextArea @bind-Value="starship.Description" />
            </label>
        </p>    
        <p>
            <label>
                Maximum Accommodation:
                <InputNumber @bind-Value="starship.MaximumAccommodation" />
            </label>
        </p>
        <p>
            <label>
                Engineering Approval:
                <InputCheckbox @bind-Value="starship.IsValidatedDesign" />
            </label>
        </p>
        <p>
            <label>
                Production Date:
                <InputDate @bind-Value="starship.ProductionDate" />
            </label>
        </p>
    
        <button type="submit">Submit</button>
    
       
    </EditForm>
    
    @code {
        private Starship starship = new() { ProductionDate = DateTime.UtcNow };
    
        private void HandleValidSubmit()
        {
            Logger.LogInformation("HandleValidSubmit called");
            //I have to check for some custom validation spefic to this form and end result is 
            //i might have to show a validation message against the property    ProductionDate
            //      Please choose a date before 01/01/2021 or something similar
            
            Does Blazor does have anything like 
            ModelState.AddModelError("Key","Error message"); 
            
            
        }
    }

我们有类似于ModelState.AddModelError的东西吗?

EN

回答 1

Stack Overflow用户

发布于 2022-07-01 09:34:28

可以添加自定义验证处理程序,如本达克中的示例所示。首先,不要将您的模型传递给EditForm,而是一个允许您访问某些生命周期方法的EditContext。以下是相关代码:

OnParametersSetAsync

代码语言:javascript
复制
// Create EditContext
editContext = new EditContext(assignment);

// Create additional message store for the custom validation messages
validationMessageStore = new(editContext);

// Add additional validation handler
editContext.OnValidationRequested += OnValidationRequestedAsync;

以下是自定义验证处理程序的代码:

代码语言:javascript
复制
private async void OnValidationRequestedAsync(object sender, ValidationRequestedEventArgs e)
{
    // clear previous error messages
    validationMessageStore.Clear();

    // check DB if Title already exists
    bool exists = await myService.IsPresent(myModel.Title);

    // While waiting for this async process, OnValidSubmit gets called
        
    if (exists)
    {
        // yes, so add a validation message
        validationMessageStore.Add(() => myModel.Title, "The Title is already used.");

        // inform ValidationSummary that a new error message has been added
        editContext.NotifyValidationStateChanged();
    }
}

正如问题中提到的#72827137,似乎有必要在OnValidSubmit中执行检查。

代码语言:javascript
复制
// check DB if Title already exists
bool exists = await myService.IsPresent(myModel.Title);

if (exists)
{
    // do nothing if invalid (error message is displayed by OnValidationRequestedAsync)
    return;
}

// ...

上面链接中的示例还将Dispose中的事件处理程序取消挂钩,不确定这是否真的有必要。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69724492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档