简体   繁体   English

验证在 Blazor 服务器端不起作用

[英]Validation doesn't work on Blazor Server Side

I defined a form LogInForm.cs like this:我像这样定义了一个表单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; }
    }
}

And a login screen LogIn.razor like this:还有一个登录屏幕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();
        }
    }

}

When I'm filling (or not) the fields, it's always indicating them as valid, even though it's supposed to be not valid.当我填写(或不填写)这些字段时,它总是将它们指示为有效,即使它应该是无效的。

I checked the _Imports.razor and it got the Microsoft.AspNetCore.Components.Forms library.我检查了_Imports.razor并得到了Microsoft.AspNetCore.Components.Forms库。 I tried with Chrome and Firefox and it's always giving the same result.我尝试使用 Chrome 和 Firefox,它总是给出相同的结果。 I checked if javascript was on, and it was.我检查了 javascript 是否打开,并且确实打开了。

So what I'm doing wrong?那么我做错了什么? Is there anything to do with my code?和我的代码有什么关系吗? Do I have to add something in the Startup.cs file?我必须在Startup.cs文件中添加一些内容吗? I made a Blazor app as a tutorial using the validation system and it was working flawlessly.我使用验证系统制作了一个 Blazor 应用程序作为教程,它运行良好。

I think the problem here is simply that you've forgotten to add the component which actually performs the validation.我认为这里的问题只是您忘记添加实际执行验证的组件。 To fix that, add this line below your <EditForm Model="@logInForm" OnValidSubmit="@TryLogIn"> line:要解决此问题,请将此行添加到您的<EditForm Model="@logInForm" OnValidSubmit="@TryLogIn">行下方:

      <DataAnnotationsValidator />

Also, the [DataType] attribute is for formatting rather than validation.此外, [DataType]属性用于格式化而不是验证。 The validation annotation for an email address is [EmailAddress] , so add that too and it should work as expected. email 地址的验证注释是[EmailAddress] ,所以也添加它,它应该可以按预期工作。 More on that here .更多关于这里

As a slight aside on this;顺便说一句; I came from the WinForms world where validation often felt like an unknowable black box.我来自 WinForms 世界,在这个世界里,验证常常感觉像是一个不可知的黑匣子。 In Blazor it's really well documented in the docs and also in the code , so if you want to learn more about it at any point it's actually possible.在 Blazor 中,它在文档代码中都有很好的记录,所以如果你想在任何时候了解更多关于它的信息,它实际上是可能的。 This blog by Steve Sanderson has some really good outline info in the "Blazor's forms and validation extensibility" section. Steve Sanderson 的这篇博客在“Blazor 的 forms 和验证可扩展性”部分中有一些非常好的大纲信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM