Does anyone have some working examples of using the when and satisfies custom validation rules?
I have the basics working, but when I need to do something more complex I am failing to get it working.
I have a field that if checked i want to be sure that they enter something in a text field.
In this case the model contains a boolean if fuel was purchased, and a string field for the content.
I want to check if fuel was purchased (true) and then make sure at least 1 character was entered into the text field.
Example:
ValidationRules
.ensure('fuelAmount').required().minLength(1).when((o: PaveTimecardModel) => o.fuelPurchased)
.withMessage("Purchased Fuel checked, no entry detected.")
.on(PaveTimecardModel);
And example of the satisfies that I cannot get working is as follows.
I have a drop down to select the rental type that must not be the default value of Not selected (0).
I have secondary drop down Instructor that requires a selection if the rental type is dual (3).
In this case the first rule validates correctly if a selection is made adding and removing the validation message correctly, but when the selection is dual, it should call out a validation error if no instructor is selected, but its not.
example:
ValidationRules
.ensure('rentalTypeId').satisfies((val, o: PaveTimecardModel) =>
{
if (o.rentalTypeId === RENTAL_NOSELECTED)
{
return false;
}
return true;
})
.then()
.withMessage("Rental Type needs to be selected.")
.ensure('instructorId').satisfies((val, o: PaveTimecardModel) =>
{
if (o.rentalTypeId === RENTAL_DUAL && o.instructorId === NOINSTRUCTOR)
{
return false;
}
return true;
})
.withMessage("Rental Type [Dual] requires instructor to be selected.")
.on(PaveTimecardModel);
Anything looking out of place here?
Thanks bunches!