Problem/Motivation
I am creating a module which includes the basics that are needed for a Headless Drupal setup. So in the front-end there is React and in the back-end Drupal. At this moment when a user fills in a form and clicks on submit the form values and the form id that is given by webforms are getting posted to a custom rest endpoint.
With the following link i was able to create a submission with these values and form_id. Webform 8.x-5.x: How to programmatically create a submission
But the problem is that there is no validation. I am trying to figure out how i can manually trigger the validation by myself, but can't seem to figure it out. Is there even a way to validate those values?
Comments
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI think would need to programmatically execute \Drupal\webform\WebformSubmissionForm and grab the $form_state->getErrors() after calling ::validateForm().
Below is code that IS NOT WORKING but illustrates the concept of programmatically building and validating a form.
Comment #3
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI found this helper to programmatically submit a form, but it does not work for entity forms, which requires some addition information and dependancies.
Comment #4
Nylsoo CreditAttribution: Nylsoo commentedHi Jacob,
Thanks for the fast response! Combining the Helper and your code i got my code working.
Comment #5
Nylsoo CreditAttribution: Nylsoo commentedComment #6
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@Nylsoo That is great. That is a lot less code than I thought would be needed.
Could you please update the original Webform 8.x-5.x: How to programmatically create a submission recipe to include your code snippet?
Thanks,
~jake
Comment #7
Nylsoo CreditAttribution: Nylsoo commented@jrockowitz:
I have updated Webform 8.x-5.x: How to programmatically create a submission, could you check if this is as you like?
Cheers,
Niels
Comment #8
imclean CreditAttribution: imclean commentedThis type of function will only become more common in the future. A REST endpoint for submitting webforms would be ideal, Currently it looks like it might be possible to update (create?) a submission:
Being able to POST to
/form/{webform}
would be fantastic. (Incidentally, at/admin/config/services/rest
it's still listed as/webform/{webform}
.Comment #9
imclean CreditAttribution: imclean commentedMy above comment doesn't make sense. POSTing via REST to the webform URL
webform/webform_id
would involve updating the webform, not creating a submission.Comment #10
imclean CreditAttribution: imclean commentedI'm having a go at wrapping #4 inside a REST resource in a custom module.
Comment #11
imclean CreditAttribution: imclean commentedHere it is: https://www.drupal.org/sandbox/imclean/2854742
Initial release only. There's no permissions checking or validation so may need some work there. Form errors are picked up but not handled yet.
Comment #12
imclean CreditAttribution: imclean commentedI've added basic support for retrieving the webform elements via REST to allow the backend to control the content and config on the front end. For example, you could grab a node with a webform with
GET /node/nid?_format=hal_json
then use the webform field'starget_id
to load the webform:GET /webform_rest/elements/webform_id?_format=hal_json
.