I wanted to implement the custom validation, but promise based. I was not able to introduce it via custom validator, I think its not asynchronous. (maybe I'm wrong? Can I invoke pnp and chain it in validator?)
Documentation states that spBeforeSave can return promise.
Since throw error does not stop the process of saving the form for me, I had to do a lot of workarounds and:
introduce global variable storing validation error,
introduce custom validator that checks this global variable and shows error if any present,
disable Save button and provide custom button that invokes my own 'validateMyForm()' function, where I can asynchronously query rest services, and chain next promises that leads to form saving (custom validator before, naturally).
Maybe there is something wrong in my understanding, but I could not find anything on forum nor documentation about async validation with promises.
You can create add a custom validation that checks if the form is valid or not, and prevent form submission if there are any errors.
Please find more information in our documentation here.
fd.validators;
fd.validators.push({
name: 'MyCustomValidator',
error: "Age must be 18 or over in order to subscribe",
validate: function(value) {
if (fd.field('Age').value < 18
&& fd.field('PaymentModel').value == 'Subscription')
return false;
return true;
}
});