您好,欢迎访问一九零五行业门户网

如何使用 Fluent Validation 检查 C# 中属性的最小长度和最大长度验证?

maxlength validatorensures that the length of a particular string property is no longer than the specified value.
only valid on string properties
string format args:
{propertyname} = the name of the property being validated
{maxlength} = maximum length
{totallength} = number of characters entered
{propertyvalue} = the current value of the property
minlength validatorensures that the length of a particular string property is longer than the specified value.
only valid on string properties
{propertyname} = the name of the property being validated
{minlength} = minimum length
{totallength} = number of characters entered
{propertyvalue} = the current value of the property
examplestatic void main(string[] args){ list errors = new list(); personmodel person = new personmodel(); person.firstname = "testuser444"; person.lastname = "ttt"; personvalidator validator = new personvalidator(); validationresult results = validator.validate(person); if (results.isvalid == false){ foreach (validationfailure failure in results.errors){ errors.add(failure.errormessage); } } foreach (var item in errors){ console.writeline(item); } console.readline(); }}public class personmodel{ public string firstname { get; set; } public string lastname { get; set; }}public class personvalidator : abstractvalidator{ public personvalidator(){ rulefor(p => p.firstname).maximumlength(7).withmessage("maximumlength must be 7 {propertyname}") ; rulefor(p => p.lastname).minimumlength(5).withmessage("minimumlength must be 5 {propertyname}"); }}
输出maximumlength must be 7 first nameminimumlength must be 5 last name
以上就是如何使用 fluent validation 检查 c# 中属性的最小长度和最大长度验证?的详细内容。
其它类似信息

推荐信息