1

The webform module is being used for a form that takes a payment. The data is validated as much as it can and then in a submit handler it is finally sent to the payment processor.

The result can be a failure (wrong card number, or no funds etc.), which should result in the form being re-presented giving the user the opportunity to check their fat fingers didn't type in the wrong card details, try another card etc.

In a vanilla Drupal form I could do a $form_state['redirect'] = FALSE; and it would return the form as-is, but this does not work when working within webform because it deals with that itself.

So this is a weird one because it's basically validation done in a submit handler. This is because we need to make sure everything's valid before submitting it to the third party, but there are clearly two ways to frame this question:

  1. As the title says, is it possible for a submit handler to tell webform to stay on this page and not consider it complete?

    or

  2. If I move the submission code into a validation handler, how can I ensure my validation handler happens after all the others, and then check that there aren't any other validation errors before I proceed with the payment submission?

1
  • I only know from Form API where you could add an empty validate function and inside there you put a form_set_error() after a dpm($form_state) to debug your values. The form_set_error() simply stops the process, so the submit won't get fired.
    – leymannx
    Aug 24, 2015 at 16:38

1 Answer 1

7

First is I'm not sure if it's best practice to do validations in the submit handler because as you've said, you need to validate first. So I can only answer your second question.

To make your validation run after every validation present for that form, you need to add it in the end of the #validate attribute. Example is like:

$form['submit']['#validate'][] = 'your_last_needed_validation_func';

To check if there are validation errors before your validation function, you can use the form_get_errors function.

2
  • Yes, true, it's just awkward when validation is external and atomically bound to submission in the external system! Either the processor will return 'invalid', or not only will it return 'valid', it will also have taken payment, which could be considered a submission! Thanks for your answer, this is the way I'm going at the mo. Aug 24, 2015 at 17:23
  • Yeah! You're right! It's kinda scary also that if we need some "abort" scenario it might be very late and difficult to handle on the submission process because it's submission already. Glad to help! :) Aug 24, 2015 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.