This is my ViewModel class:
public class CreatePersonModel
{
public string Name { get; set; }
public DateTime DateBirth { get; set;
As Stewart mentioned, it's not possible to use FluentValidation alone to get in front of the model binding in this way. I'd offer up two ideas/suggestions though:
public CreatePersonValidator()
{
RuleFor(courseOffering => courseOffering.StartDate)
.Must(BeAValidDate).WithMessage("Start date is required");
//....
}
private bool BeAValidDate(DateTime date)
{
return !date.Equals(default(DateTime));
}
Have a look at the Fluent Validation documentation on GitHub:
https://github.com/JeremySkinner/FluentValidation/wiki
Try adding a RegEx Validator to ensure that the user's input (a string) can be parsed as a date correctly, prior to applying the Less Than Validator.
EDIT
Having run few test cases and looked at the source code for Fluent Validator I concede that the above approach won't work.
The standard error you get is added during the Model Binding phase, which happens before the fluent validation framework can access and check the model.
I assumed that the framework's authors had been clever and were injecting their validation code into the model binding phase. Looks like they aren't.
So the short answer is what you want to do does not appear to be possible.
Try this one
RuleFor(f =>
f.StartDate).Cascade(CascadeMode.StopOnFirstFailure).NotEmpty()
.Must(date => date != default(DateTime))
.WithMessage("Start date is required");