I have never heard of FluentValidation until today while I was working on a project, so I have run into an issue. I have this..
RuleFor(x=>x.Company)
.NotEmpty()
.WithMessage("Company Required");
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage(FirstName Required");
RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("LastName Required");
and a bunch of other RuleFor statements.What I need to do is..
If Company field is empty then require validation for FirstName and LastName, but if Company field is not empty then don't apply validation to FirstName and LastName
I have no idea where to start.
EDIT
I tried the When condition and came up with this
When(x => x.Company == "" || x.Company == null, () =>
{
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage("FirstName Required");
RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("LastName Required");
});
and I "think" that should have set off the validation for FirstName and LastName, but it didn't.
Then I tried this way
When(x.Company.length == 0, () =>
{
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessage("FirstName Required");
RuleFor(x => x.LastName)
.NotEmpty()
.WithMessage("LastName Required");
});
and the same thing happened, the FirstName and LastName validation didn't happen.