spBeforeSave throw Error does not prevent from saving

Hi,

Im encountering a problem with custom validation.

I have just placed a simple code from an example (JS Current Form tab).

fd.spBeforeSave(function () {
    throw Error('test');
});

This does not prevent the form from saving. What might be the case?

Hello @jedrzej.dankowski,

How do you want to validate the form?
Do you want to check the field values before submitting the form?

Hi @mnikitina,

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.

Regards, J.

Hello @jedrzej.dankowski,

Yes, you are tight, the validators do not support async methods.

Would you consider a paid support for adding async validation? Please contact us by email [email protected] for more details.

How do I stop a form from saving if it doesn't validate?

Hello @smithme,

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;
    }
});