我想使用流利的验证器验证bool属性。我应该使用哪种方法?
.NotNull()和.NotEmpty()函数不起作用。
谢谢。
你应该使用.NotNull()
。
NotEmpty()
将只接受true
作为有效财产。 NotNull()
将把true
和false
作为有效的财产。
为了使用流畅的验证器验证布尔值:
创建以下规则
RuleFor(x => x.BooleanProperty)
.Must(ValidateBoolean)
.WithErrorCode("Boolean Validation Failed");
定义谓词验证器
private bool ValidateBoolean(T arg1, bool arg2, PropertyValidatorContext arg3)
{
// validate arg2, return true if validation successful or false if failed
throw new System.NotImplementedException();
}
请看看this question。它成功回答了,我猜这就是你要找的东西。
在其中,他们创建流畅的验证器来检查是否选择了@Html.CheckBox
。如果我理解你的问题,这正是你正在寻找的。
如果有帮助,请告诉我。 - 谢谢
尝试使用.Equal(true)
- 那就做了。