提问者:小点点

仅在单击字段后启用必需的属性验证


如果我具有以下viemodel属性:

public class ExampleViewModel
{
    [Required(ErrorMessage = "The email field is required.")]
    [EmailAddress(ErrorMessage ="Please enter a valid email address")]
    public string Email { get; set; }
    [Required(ErrorMessage = "The first name field is required.")]
    [RegularExpression("^[a-zA-Z\\-]+$", ErrorMessage="Please enter a valid name")]
    public string Forename { get; set; }
    [Required(ErrorMessage = "The last name field is required.")]
    [RegularExpression("^[a-zA-Z\\-]+$", ErrorMessage = "Please enter a valid name")]
    public string Surname { get; set; }
}

那么我如何只在前端启用必填字段,只有在字段被点击和点击离开之后?

当前,错误消息会立即显示给查看器:示例:

前端剃刀页正在使用blazorise验证,它仅验证是否已填充所有字段:

                        <Form method="post">
                            <Validations Mode="ValidationMode.Auto" Model="@ExampleViewModel" ValidatedAll="IsInvalid">
                                <Validation>
                                    <Blazorise.Field>
                                        <Blazorise.FieldLabel Class="is-bold">Email address</Blazorise.FieldLabel>
                                        <Blazorise.TextEdit Placeholder="Enter email" @bind-Text="@ContactDetails.Email" MaxLength=40>
                                            <Feedback>
                                                <ValidationError />
                                            </Feedback>
                                        </Blazorise.TextEdit>
                                    </Blazorise.Field>
                                </Validation>
                                <Row>
                                    <Blazorise.Title Size="10">Your Details</Blazorise.Title>
                                </Row>
                                <Validation>
                                    <Blazorise.Field>
                                        <Blazorise.FieldLabel Class="is-bold">First name</Blazorise.FieldLabel>
                                        <Blazorise.TextEdit Placeholder="John" @bind-Text="@ContactDetails.Forename" MaxLength=40>
                                            <Feedback>
                                                <ValidationError/>
                                            </Feedback>
                                        </Blazorise.TextEdit>
                                    </Blazorise.Field>
                                </Validation>
                                <Validation>
                                    <Blazorise.Field>
                                        <Blazorise.FieldLabel Class="is-bold">Last name</Blazorise.FieldLabel>
                                        <Blazorise.TextEdit Placeholder="Doe" @bind-Text="@ContactDetails.Surname" MaxLength=40>
                                            <Feedback>
                                                <ValidationError />
                                            </Feedback>
                                        </Blazorise.TextEdit>
                                    </Blazorise.Field>
                                </Validation>

共1个答案

匿名用户

也许这会有所帮助:RequiredIf条件验证属性

如果您想使用数据注释,您可以在视图模型中创建一个属性boolean,它指示您是否应该检查required。

public bool HasUserClicked { get; set; }

[RequiredIf(nameof("HasUserClicked",true)
public string MyProperty{ get; set; }

当用户单击时,您将切换该布尔值。

编辑不确定它将与Blazor一起工作顺便。。。 如果您想要更多的条件验证,您也许应该考虑FluentValidation。