Pause submission and execute custom javascript on successful validattion

The subject line pretty much says it all. I don’t need any custom validation logic… when the form passes whatever standard validation I set, I need to perform some client-side operations before processing the form (i.e. saving entry, sending notifications and/or displaying confirmation). There doesn’t appear to be a hook at that specific point (gform_pre_submission_filter looked promising, but I can’t see how I can run anything client-side there… or stop the submission), but if anyone could at least point me in the right direction (a lot of this documentation sails right over my head - if I at least knew I was reading about the right topic, it would make a huge difference), I’d be most appreciative. Thanks!!

Hi Dave. Client side validation is not a feature of Gravity Forms. You can do that client-side with jQuery or JavaScript. We don’t have any documentation for that, but there are some resources online.

Here is one about the jQuery Validate plugin:

I hope that helps, and I will leave this open in case anyone wants to add anything.

I"m not trying to do client-side validation. I’m trying to run some JavaScript after gravity forms does its server-side validation. SO rather than going on to save the entry and show the confirmation to the user, I just want to return to the form (as if validation had failed)… and I need some hook or event I can listen for and execute my script off of. Does that make sense?

Hi Dave. Exactly when in the process do you need to execute your script? After Gravity Forms validation, before saving the entry? At that point, you are not client side, you are server side, so your code would be in PHP.

I like to refer back to this reference to determine which hook or filter occurs at the best time:

I think I would use gform_validation or gform_field_validation. That is when Gravity Forms validation runs and yours can run as well. The documentation for gform_field_validation, for example, shows this code:

if ( $result['is_valid'] && intval( $value ) > 10 ) {

That means your code would only run if the validation result (from Gravity Forms) is already is_valid (and in this case, the value is greater than 10.) Without knowing exactly what you need to do, gform_validation or gform_field_validation are probably the correct place to do it.

Gotcha -if I embed my tag in that php block, will it be written to the page and executed?

Probably not. I believe you would need to use PHP at that point, because it’s not really ‘on’ that page any more, it’s in between pages (the form page and the confirmation page.)

I see. Is there a way to return to the form as if there was an invalid field?

Yes, using the gform_validation filter or gform_field_validation filter, you can return a validation error without any condition. You would set $result['is_valid'] = false; in your code before testing anything. That will return you to the form with a default validation error message and you could run your JavaScript code there.

Great. I can definitely make that work. Thanks!!

1 Like
© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.