We can use the setIsValid method for validating field values in Model-driven apps. This method helps ensure that the data entered by users meets the required criteria before it’s processed or saved.
The setIsValid method is used to set the validity of a column’s value. It can mark a field as valid or invalid based on custom validation logic.
formContext.getAttribute(arg).setIsValid(bool, message);
bool: A Boolean value. Set to false to mark the column value as invalid, and true to mark it as valid.
message: (Optional) A string containing the message to display when the value is invalid.
Below we are using the setIsValid method in the function that ensures that the “End Date” is earlier than or equal to the “Start Date”, else it will mark the “End Date” as invalid.
function validateDates(executionContext) {
var formContext = executionContext.getFormContext();
var startDate = formContext.getAttribute("custom_startdate");
var endDate = formContext.getAttribute("custom_enddate");
if (startDate && endDate && endDate.getValue() <= startDate.getValue()) {
endDate.setIsValid(false, "End Date must be after Start Date.");
} else {
endDate.setIsValid(true);
}
}
We have it registered in the On Change of the ‘Start Date’ and ‘End Date’ fields.
Here if we try saving the record, if the End Date is smaller than the Start Date, we will get the message specified.

Hope it helps..

















