ตัวตรวจสอบความยาวสูงสุด
ตรวจสอบให้แน่ใจว่าความยาวของคุณสมบัติสตริงเฉพาะไม่เกินค่าที่ระบุ
ใช้ได้เฉพาะกับคุณสมบัติของสตริงเท่านั้น
args รูปแบบสตริง:
{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