Custom form RESTful submission action


(Mahearnpad) #1

Hi

 

How can I display error messages returned from an external API. on my custom form?  

 

The setup is:

Custom form (user registration), with a submission action set to "call REST resource"

REST resource POSTs form data to external API

External API returns error data (e.g. if username exists etc)

 

Need to display this error data to the user - how to do this???

 

TIA

M


(Tbaatar) #2

You can display the Error message via a Trigger?


(Mahearnpad) #3

thanks, I'll have a look into triggers


(Bart Banda) #4

You can unfortunately only do that natively with SOAP form submission action at the moment which is a shame, we are planning on adding that to the REST action as well. 

 

Could you submit the REST call via AJAX before the form is submitted to Matrix? Then based on the response from the AJAX call, you can stop the submission, or put a failed value into a matrix form field, submit the form as per normal, and let the error handling be handled by the matrix form. 


(Mahearnpad) #5

Damn!  Thanks anyway Bart.

 

So now our option is to submit to a Matrix REST resource using AJAX, and do as you say.  OK, I'll try that.

 

Michael


(Mahearnpad) #6

If we have a REST resource as nested content within some JavaScript code, does the REST resource have access to the variables in the JavaScript? It doesn't appear to, on testing.


(Bart Banda) #7

I think it depends on where you nest your rest asset in. If you just do it on the first page of the form, then it will only have access to POST variables that are sent with the form after submission. But this wouldn't help you anyway.

 

You could maybe do something on the confirmation screen of the form, so print the response of the REST call on the confirmation screen, and based on that value, you either show or hide the Next/Confirm button and an error text. If there is an error, display message and hide next button and tell the user they have to go back to page 1 to choose a different username. 

 

I would still try and do this with AJAX on the first page to improve the usability of it all, but that would be a way to handle it all server side.

 

Do you think that would work?


(Pheasley) #8

Hi Bart,

 

Thanks for your help, I work with Michael and we're currently implementing your AJAX solution, which is fine but it means we lose Squiz spam prevention and validation. I'm wondering if it's even worth having a custom form or just a standard HTML form. Anyway, I digress.

 

Regarding the REST submission action validation error message, if there was a way we could insert the response body into the error message like SOAP allows we could do some js magic on the front end to use that, is there any undocumented keyword replacement in the error message? Or if it's in development could we talk to Squiz about doing a custom code change for our instance?

 

Thanks,

 

Paul. 


(Bart Banda) #9

Hi Bart,

 

Thanks for your help, I work with Michael and we're currently implementing your AJAX solution, which is fine but it means we lose Squiz spam prevention and validation. I'm wondering if it's even worth having a custom form or just a standard HTML form. Anyway, I digress.

 

Regarding the REST submission action validation error message, if there was a way we could insert the response body into the error message like SOAP allows we could do some js magic on the front end to use that, is there any undocumented keyword replacement in the error message? Or if it's in development could we talk to Squiz about doing a custom code change for our instance?

 

Thanks,

 

Paul. 

 

To help combat SPAM, you can have a hidden text field on the form that gets populated with a specific value if the AJAX response is successful. You can then do error checking on this form field alone and make sure that the value is correct otherwise the form won't submit, that's one way. 

 

I just remembered that there is a validation function added to the REST Call submission action that you can use as validation and error displaying on the form, http://manuals.matrix.squizsuite.net/custom-form/chapters/form-contents#call-rest-resource, it was added in 4.14.2 though so not sure what version of Matrix you are up to. 

 

I don't know if a keyword to print the response body from the submission action, I don't think there is one, but this is one of the things we want to add in the near future.