如何在 fluentvalidation 中向 属性 validator İsvalid 发送消息?
How can I send a message to property validator İsvalid in fluentvalidation?
你好,fluent validation已经更新到10.3.6版本了,但是之前在properyvalidator中的SetMessage函数不能用了,我应该用什么代替呢?
public class TcknOrVknPropValidator : PropertyValidator
{
public TcknOrVknPropValidator() : base()
{
}
protected override bool IsValid(PropertyValidatorContext context)
{
var propertyValue = context.PropertyValue as string;
if (string.IsNullOrWhiteSpace(propertyValue))
{
SetErrorMessage("Tc veya Vergi No Geçersiz");//this function not found now
return false;
}
if (propertyValue.Length < 10 || propertyValue.Length > 11)
{
SetErrorMessage("Tc Veya Vergi No Eksik Yada Hatalı");
return false;
}
return true;
}
在新版本中,它的用法如下
public class VisitIdValidator<T, TCollectionElement> : PropertyValidator<T, TCollectionElement>
{
public VisitIdValidator()
{
}
public override bool IsValid(ValidationContext<T> context, TCollectionElement visitId)
{
if (visitId==null || string.IsNullOrEmpty(visitId.ToString()))
{
context.MessageFormatter.AppendArgument("NotNull", visitId);
return false;
}
return true;
}
public override string Name => "ListCountValidator";
protected override string GetDefaultMessageTemplate(string errorCode)
=> "{PropertyName} notnull";
}
使用
RuleFor(x => x.VisitId).SetValidator(new VisitIdValidator<SavePhoneRequestModel,string>());
你好,fluent validation已经更新到10.3.6版本了,但是之前在properyvalidator中的SetMessage函数不能用了,我应该用什么代替呢?
public class TcknOrVknPropValidator : PropertyValidator
{
public TcknOrVknPropValidator() : base()
{
}
protected override bool IsValid(PropertyValidatorContext context)
{
var propertyValue = context.PropertyValue as string;
if (string.IsNullOrWhiteSpace(propertyValue))
{
SetErrorMessage("Tc veya Vergi No Geçersiz");//this function not found now
return false;
}
if (propertyValue.Length < 10 || propertyValue.Length > 11)
{
SetErrorMessage("Tc Veya Vergi No Eksik Yada Hatalı");
return false;
}
return true;
}
在新版本中,它的用法如下
public class VisitIdValidator<T, TCollectionElement> : PropertyValidator<T, TCollectionElement>
{
public VisitIdValidator()
{
}
public override bool IsValid(ValidationContext<T> context, TCollectionElement visitId)
{
if (visitId==null || string.IsNullOrEmpty(visitId.ToString()))
{
context.MessageFormatter.AppendArgument("NotNull", visitId);
return false;
}
return true;
}
public override string Name => "ListCountValidator";
protected override string GetDefaultMessageTemplate(string errorCode)
=> "{PropertyName} notnull";
}
使用
RuleFor(x => x.VisitId).SetValidator(new VisitIdValidator<SavePhoneRequestModel,string>());