
Ensures 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
Ensures 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
static 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 Name MinimumLength must be 5 Last Name
以上就是如何使用 Fluent Validation 检查 C# 中属性的最小长度和最大长度验证?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号