I am using the FluentValidation package in my ASP.net project. I would like to know how to pass a parameter to the Validator constructor.
Here is what I would like to do with my validator. Notice the string MsgParam in the constructor:
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);
}
}
And my model, where I do not know if I can pass anything using data annotation:
// 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; }
}
Is it possible to do such a thing?