2016-09-16 60 views
0

客戶端驗證正在'公司名'工作正常,但對於繼承類,即運輸&結算其不起作用。請建議解決方案。FluentValidation客戶端驗證不適用於繼承類

[Validator(typeof(ClientValidator))] 
public class Client 
{ 
    public string CompanyName{get;set;} 

    private volatile Contact Shipping = null; 
    private volatile Contact Billing = null; 

} 

public class Contact : Address 
{ 

} 

public class Address 
{ 

public String Name{get;set;} 

    public String Phone{get;set;} 
} 


public class ClientValidator : AbstractValidator<Client> 

{ 

     public ClientValidator() 

{ 

RuleFor(x => x.CompanyName).NotNull().WithMessage("Required").Length(1, 15).WithMessage("Length issue."); 


      RuleFor(x => x.Shipping.Name).NotNull().WithMessage("Required").Length(1, 15).WithMessage("Length issue."); 

      RuleFor(x => x.Shipping.Phone).NotNull().WithMessage("Required").Length(1, 15).WithMessage("Length issue."); 
      RuleFor(x => x.Billing.Name).NotNull().WithMessage("Required").Length(1, 15).WithMessage("Length issue."); 
      RuleFor(x => x.Billing.Phone).NotNull().WithMessage("Required").Length(1, 15).WithMessage("Length issue."); 
     } 
    } 

回答

0

對於包含對象fluentvalidation不起作用,因爲您已實施。

爲此,請參考流利驗證的複雜屬性驗證,並嘗試使用下面的代碼。

[Validator(typeof(ClientValidator))] 
public class Client 
{ 
    public string CompanyName{get;set;} 
    private volatile Contact Shipping = null; 
    private volatile Contact Billing = null; 
} 

public class Contact : Address 
{ 

} 

public class Address 
{ 
    public String Name{get;set;} 
    public String Phone{get;set;} 
} 

public class AddressValidator : AbstractValidator<Address> 
{ 
    public ClientValidator() 
    { 
     RuleFor(x => x.Name).NotNull().WithMessage("Required").Length(1, 15).WithMessage("Length issue."); 
     RuleFor(x => x.Phone).NotNull().WithMessage("Required").Length(1, 15).WithMessage("Length issue."); 
    } 
} 

public class ClientValidator : AbstractValidator<Client> 
{ 
    public ClientValidator() 
    { 
     RuleFor(x => x.CompanyName).NotNull().WithMessage("Required").Length(1, 15).WithMessage("Length issue."); 
     RuleFor(x => x.Shipping).SetValidator(new AddressValidator()); 
     RuleFor(x => x.Billing).SetValidator(new AddressValidator()); 
    } 
} 
+0

感謝它的工作。 –

相關問題