我在我的 ASP.net 项目中使用FluentValidation包。我想知道如何将参数传递给 Validator 构造函数。
这是我想用我的验证器做的事情。注意string MsgParam
构造函数中的:
public class RegisterModelValidator : AbstractValidator<RegisterModel>
{
public RegisterModelValidator(string MsgParam) // <= Here
{
RuleFor(x => x.UserName)
.NotNull()
.WithMessage(MsgParam);
RuleFor(x => x.Password)
.NotNull()
.Length(6, 100);
RuleFor(x => x.ConfirmPassword)
.Equal(x => x.Password);
}
}
还有我的模型,我不知道是否可以使用数据注释传递任何东西:
// Find a way to pass a string to the validator
[FluentValidation.Attributes.Validator(typeof(RegisterModelValidator))]
public class RegisterModel
{
public string UserName { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
}
有可能做这样的事情吗?