I need some way with Form API to process a credit card after Drupal validates the form but before the submit function fires so that I can bounce the user back to the form with fields highlighted if their credit cards fails processing (i.e. expired card, declined, etc.). The only way I've found to do this is to manually validate all of the fields in my validate function and the process the card at the end of the same function if all of the previous field checks pass. Obviously this is kludgey as hell and is now preventing me from adding more validation to the form with a form_alter in another module (which I now need to do on some sites). What is the proper Drupal way for handling this workflow? I can't find anything in the API docs about it.
By seanr on
Comments
Technically, you are doing it
Technically, you are doing it right. The credit card being processed is part of the validation. If it isn't processed, the form data hasn't been validated, and it will bounce back with an error.
However, there are various ways of doing what you want. You could add another validation function:
This will call the function 'credit_card_validation' after the normal form_api validation function. But I suspect this isn't what you want. Probably what I would do is this in the submit function:
This will check the credit card at the start of the submission script, and if it doesn't pass, it will set an error and rebuild the form with all the submitted data. If it does pass, it will process the script normally.
Contact me to contract me for D7 -> D10/11 migrations.