I am trying to use Fluent Validation and it seems easy to use at the beginning but now there is some problem. I need to validate a SignIn view model, shown below:
public SignInViewModelValidator(IMembershipService membershipService)
{
_membershipService = membershipService;
RuleFor(x => x.EMail).NotEmpty().EmailAddress();
RuleFor(x => x.Password).NotEmpty().Length(6, 20);
Custom(x =>
{
var user = _membershipService.ValidateUser(x.EMail, x.Password);
if (user == null)
return new ValidationFailure("EMail", "Your E-Mail Address or password was invalid.");
return null;
});
}
But I'm getting all the errors at once, like this:
- 'E Mail' should not be empty.
- Your E-Mail Address or password was invalid.
- 'Password' should not be empty.
How can I change this behavior to not check the Custom validation rule when the other rules are invalid? In other words, it should only check the Custom validation rule when 'EMail' and 'Password' fields are valid.