【问题标题】:Validation doesn't work on Blazor Server Side验证在 Blazor 服务器端不起作用
【发布时间】:2019-11-15 12:26:13
【问题描述】:

我这样定义了LogInForm.cs 的表单:

using System.ComponentModel.DataAnnotations;

namespace PollingInstitute.Data.LogIn
{
    public class LogInForm
    {
        [Required]
        [DataType(DataType.EmailAddress, ErrorMessage = "This field require a mail adress (example : abc@xyz.com)")]
        public string Mail { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
}

还有一个像这样的登录屏幕LogIn.razor

@page "/login"
@using PollingInstitute.Data.LogIn
@inject LogInService _LogInService

<h3>Login</h3>

<EditForm Model="@logInForm" OnValidSubmit="@TryLogIn">

    <div>
        <label>Mail Adress : </label><InputText @bind-Value="@logInForm.Mail"></InputText>
        <ValidationMessage For="@(()=>logInForm.Mail)"></ValidationMessage>
    </div>

    <div>
        <label>Password : </label><InputText @bind-Value="@logInForm.Password"></InputText>
        <ValidationMessage For="@(()=>logInForm.Password)"></ValidationMessage>
    </div>

    <div>

        @if (!IsDisabled)
        {
            <button type="submit">Submit</button>
        }
        else
        {
            <label>Please wait...</label>
        }
    </div>
    <ValidationSummary></ValidationSummary>
</EditForm>

@code {

    LogInForm logInForm = new LogInForm();
    protected bool IsDisabled { get; set; } = false;

    async void TryLogIn()
    {
        if (IsDisabled == true)
        {
            IsDisabled = true;
            StateHasChanged();
            bool result = (await _LogInService.TryLogIn(logInForm));
            Console.WriteLine("Logging status : " + (result ? "Sucess" : "Failure"));
            IsDisabled = false;
            StateHasChanged();
        }
    }

}

当我填写(或不填写)这些字段时,它总是将它们指示为有效,即使它应该是无效的。

我检查了_Imports.razor,它得到了Microsoft.AspNetCore.Components.Forms 库。 我尝试使用 Chrome 和 Firefox,它总是给出相同的结果。 我检查了 javascript 是否已打开,确实如此。

那么我做错了什么?我的代码有什么关系吗?我必须在Startup.cs 文件中添加一些内容吗?我使用验证系统制作了一个 Blazor 应用作为教程,它运行良好。

【问题讨论】:

    标签: c# asp.net asp.net-core blazor-server-side


    【解决方案1】:

    我认为这里的问题只是您忘记添加实际执行验证的组件。要解决此问题,请将此行添加到您的 &lt;EditForm Model="@logInForm" OnValidSubmit="@TryLogIn"&gt; 行下方:

          <DataAnnotationsValidator />
    

    另外,[DataType] 属性用于格式化而不是验证。电子邮件地址的验证注释是 [EmailAddress],所以也添加它,它应该可以按预期工作。更多关于 here.

    顺便说一句;我来自 WinForms 世界,在那里,验证常常感觉像是一个不可知的黑匣子。在 Blazor 中,in the docsin the code 都有很好的文档记录,因此如果您想在任何时候了解更多关于它的信息,它实际上是可能的。 This blog by Steve Sanderson 在“Blazor 的表单和验证可扩展性”部分中有一些非常好的大纲信息。

    【讨论】:

    • 现在可以使用了,谢谢。奇怪的是,[DataType(DataType.EmailAdress)] 属性不起作用,但我肯定误解了它的功能。
    • @PepperTiger DataType 用于格式化而不是验证。验证注释是[EmailAddress],所以也添加它,它应该可以按预期工作。更多信息在这里:stackoverflow.com/a/8989157/1879019
    • @PepperTiger 我找不到仅包含验证属性的列表,但是如果您在此列表中的描述中查找包含“验证”的属性,您应该会有一个很好的主意:@ 987654326@
    猜你喜欢
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2020-04-23
    • 2020-07-26
    相关资源
    最近更新 更多