可以将自定义验证消息添加到我的必填字段吗?

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

Is it possible to add a custom validation message to my required field?

问题

我有一个带有一些InputText的EditForm。其中一些文本框是必填的,因此它们的类使用[Required]声明。但是,其中一些文本框还使用[StringLength(100, ErrorMessage = "Forename is too long")]声明。当文本框中的名字太长时,下面的消息是我写的'Forename is too long'。当我有一个RegularExpression [RegularExpression(@"^\w+[.]\w+[@]\w+[.]\w+$",
ErrorMessage = "Email must follow fred.example@live.com structure")].然后错误消息是'Email must follow fred.example@live.com structure'。所以我认为通过添加一些验证到[Required]来修改消息会很容易:[Required(ErrorMessage = "This is required")。但是显示的消息是'The Surname field is required'。我该如何编辑这个消息,因为我已经明显没有完成工作。


<InputText id="userForename" @bind-Value="newUser.Forename">


<InputText id="userSurname" @bind-Value="newUser.Surname">


<InputText id="userEmailAddress" @bind-Value="newUser.EmailAddress">

英文:

I have an EditForm with some InputText. Some of the textboxes are required and as such in their class are declared using the [Required]. However some of them also have a [StringLength(100, ErrorMessage = "Forename is too long")]. When the forename is too long in the textbox then the message below is as I have written it 'Forename is too long'. When I have a RegularExpression [RegularExpression(@"^\w+[.]\w+[@]\w+[.]\w+$",
ErrorMessage = "Email must follow fred.example@live.com structure")]. Then the error message is 'Email must follow fred.example@live.com structure'. So I though it would be easy to add some validation to the [Required] by putting : [Required(ErrorMessage = "This is required")]. However the message displayed is 'The Surname field is required.'. How can I edit this message because what I have done has clearly not done the job.

&lt;EditForm Model=@newUser OnValidSubmit=&quot;HandleSubmit&quot;&gt;
    &lt;DataAnnotationsValidator /&gt;
    @*    &lt;ValidationSummary/&gt;*@

    &lt;label for=&quot;userForename&quot;&gt;Forename:&lt;/label&gt;
    &lt;InputText id=&quot;userForename&quot; @bind-Value=&quot;@newUser.Forename&quot;&gt;&lt;/InputText&gt;
    &lt;ValidationMessage For=&quot;@(() =&gt; newUser.Forename)&quot; /&gt;

    &lt;label for=&quot;userSurname&quot;&gt;Surname:&lt;/label&gt;
    &lt;InputText id=&quot;userSurname&quot; @bind-Value=&quot;@newUser.Surname&quot;&gt;&lt;/InputText&gt;
    &lt;ValidationMessage For=&quot;@(() =&gt; newUser.Surname)&quot; /&gt;

    &lt;label for=&quot;userEmailAddress&quot;&gt;Email Address:&lt;/label&gt;
    &lt;InputText id=&quot;userEmailAddress&quot; @bind-Value=&quot;@newUser.EmailAddress&quot;&gt;&lt;/InputText&gt;
    &lt;ValidationMessage For=&quot;@(() =&gt; newUser.EmailAddress)&quot; /&gt;
        
    &lt;button type=&quot;submit&quot; class=&quot;btn-primary&quot;&gt;Add&lt;/button&gt;

&lt;/EditForm&gt;

答案1

得分: 1

我认为你应该为TextInput创建一个类。然后,在类中,你应该添加[Required(ErrorMessage = "错误消息")]并删除&lt;ValidationMessage For="@( () => newUser.Forename)" />。然后,你可以删除@* &lt;ValidationSummary/>*@

如果你想要更多信息,你可以访问这个链接

英文:

I think you should create a class for the TextInput. Then, in the class, you should add [Required(ErrorMessage = &quot;ERROR MESSAGE&quot;)] and delete &lt;ValidationMessage For=&quot;@(() =&gt; newUser.Forename)&quot; /&gt;. Then you can delete @* &lt;ValidationSummary/&gt;*@.

If you want some more, you can visit this link

huangapple
  • 本文由 发表于 2023年2月6日 17:57:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359784.html
匿名

发表评论

匿名网友

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

确定