MaxLength 유효성 검사기
특정 문자열 속성의 길이가 지정된 값보다 길지 않은지 확인합니다.
문자열 속성에서만 유효합니다.
문자열 형식 인수:
{PropertyName} =유효성을 검사하는 속성의 이름
{MaxLength} =최대 길이
{TotalLength} =입력된 문자 수
{PropertyValue} =속성의 현재 값
최소 길이 검사기
특정 문자열 속성의 길이가 지정된 값보다 긴지 확인합니다.
문자열 속성에서만 유효합니다.
{PropertyName} =유효성을 검사하는 속성의 이름
{MinLength} =최소 길이
{TotalLength} =입력된 문자 수
{PropertyValue} =속성의 현재 값
예
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