I am using simple form fluent validations. How to enter into this rule only if it's Notes Textbox is empty and should return validation if that linq query returns a record.
If textbox is not empty it should not enter into this rule.
Here's what I have tried
RuleFor(i => i.Notes)
.NotEmpty()
.When((i) =>
{
bool result = false;
result = _DAL.GetExists<EmployeeScheduleTypes>
(q => q.Type == i.Type);
return result;
})
.WithMessage("Notes is required");
Extension method When
should be used for specifying condition when property validator should be run. In your case condition is not empty Notes
property. And validation predicate should go to Must
extension:
RuleFor(i => i.Notes)
.Must(i => _DAL.GetExists<EmployeeScheduleTypes>(q => q.Type == i.Type))
.When(i => String.IsNullOrEmpty(i.Notes))
.WithMessage("Notes is required");
This rule means: when Notes is empty then object should have existing type, otherwise set error for Notes property.