第一个枚举元素(C#)失败的枚举类型的Fluntvalidation失败
我正在使用FulentValidation在存储之前验证用户输入。每当用户选择枚举的第一个元素时,验证都会不断下降。
设想: 我的objecktmodel在其他2种枚举类型中使用了以下定义:
public enum Koerperschaft_enum
{
Privat_Person,
Vereint,
Firma,
Stiftung
}
public enum MitgliedStatus_enum
{
Mitglied,
Freispender
}
我的验证看起来像这样
public partial class MitgliedValidator : AbstractValidator<MitgliedModel>
{
public MitgliedValidator()
{
RuleFor(m => m.MitgliedStatus)
.NotEmpty()
.NotNull()
.IsInEnum();
RuleFor(m => m.Koerperschaft)
.NotEmpty()
.NotNull()
.IsInEnum();
}
}
,因为您可以看到成员失败的验证
我的对象将其成员设置为正确的枚举元素
如果输入不是枚举类型的第一个元素,则相同的验证通过。任何人都可以将我带到错误。谢谢
I’m using the fluentvalidation to validate user input before storing them. The Validation keep falling whenever the user selects the first element of the an Enum.
Scenario:
My ObjecktModel uses among other 2 Enum types as defined below:
public enum Koerperschaft_enum
{
Privat_Person,
Vereint,
Firma,
Stiftung
}
public enum MitgliedStatus_enum
{
Mitglied,
Freispender
}
My Validation looks like this
public partial class MitgliedValidator : AbstractValidator<MitgliedModel>
{
public MitgliedValidator()
{
RuleFor(m => m.MitgliedStatus)
.NotEmpty()
.NotNull()
.IsInEnum();
RuleFor(m => m.Koerperschaft)
.NotEmpty()
.NotNull()
.IsInEnum();
}
}
As you can see the validation failed by member not empty
My Object however has its members set to the right Enum element
The same validation pass if the input is not the first element of the Enum type. Can anyone please direct me to the mistake. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
notempty(notempty()
验证器检查如果不默认值(<代码> 0
未明确指定时枚举):在您的枚举中,您没有指定的明确值,因此默认值为
0
。将枚举更改为:
或删除
nonepty
验证器:如果在模型
.notnull()
中也无法删除枚举。NotEmpty()
validator checks if value is not default (0
for enums when not specified explicitly):In your enums you don't have explicit values specified so default is
0
.Change your enums to:
Or delete
NonEmpty
validators:If enum is not nullable in your model
.NotNull()
can be deleted as well.如果您使用的是非空验证器,则应注意这一点;
确保指定的属性不是null,一个空字符串或空格(或值类型的默认值,例如,int为0)。当在iEnumerable上使用(例如数组,集合,列表等)时,验证器可确保iEnumerable不是空的。
因此,您的枚举必须从1开始。如果您不设置它,默认值为0。
或者您可以添加一个
if you are using NonEmpty validator, you should pay attention to this;
Ensures that the specified property is not null, an empty string or whitespace (or the default value for value types, e.g., 0 for int). When used on an IEnumerable (such as arrays, collections, lists, etc.), the validator ensures that the IEnumerable is not empty.
Therefore your enums must start at 1. If you don't set it, the default is 0.
Or you can add None