Hey. I have a custom webform handler. It is kinda like the RemotePost handler that ships with webform, in so far as there is a remote service that the data is being set to.

I was wondering the right way to handle errors that might occur during the remote post. In my case, the remote service could pass back information such as "credit card not valid". In that case, I'd like to reload the webform and report that to the user.

I am a seasoned D7 programmer, but very new to D8, so I might be wrong about this, but I am thinking that I cannot/should not be using the webform handler mechanism for this, and that I may actually just have to just write a custom submit hander and attach that to the webform.

I think it is akin to this issue that someone posted on StackExchange

In my postSave method of my handler, if there is a way that I can fire a 'form rebuild', rather than it redirecting off to the confirmation page, it might still be able to use this neat handler mechanism, but I think even that would have downsides (as the system has probably already saved the actual webform submission by this point, which arguably might not be right).

Cheers.

Comments

AndyThornton created an issue. See original summary.

jrockowitz’s picture

For custom remote post handlers, I using a SESSION variable to store the response.

I think the best solution is going to be to add support to the webform module for webform-submission:remote-post:* tokens.

AndyThornton’s picture

I see ... kinda. so you'd store the fact there has been an error in the session, but from inside the remote post handler, how do I influence the flow so that instead of going to a 'confirmation' page, i send the user back to the submission form without losing the values (I have a version that sets the response on the FormState, but the values get lost). Or do you store their submitted values in the session too? i guess I could do that.

jrockowitz’s picture

The lazy way to store any data you want passed around is in $_SESSION.

An inline confirmation will keep the $form_state but I don't think the $form_state is passed to the confirmation template but you can access the $form_state using hook_form_alter().

AndyThornton’s picture

Thanks for the help.

I have it working. I have kept my remote service call in the webform (submit) handler. If the call results in a validation-type error, I redirect the visitor back to the form (i had to do that just by called setResponse on the form state that is passed into the handler's confirmForm method. So that the visitor does not get an empty form, I stored the form's state in the session (I wrapped it up in a service class, to make it a bit nicer, but at the end of the day, it is just using the session).

AndyThornton’s picture

Status: Active » Closed (works as designed)