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

Nylsoo created an issue. See original summary.

jrockowitz’s picture

I 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.


    // Create the submission.
    $webform_submission = WebformSubmission::create(['webform_id' => 'contact']);

    // Get the form object.
    $entity_form_object = \Drupal::entityTypeManager()->getFormObject('webform_submission', 'default');
    $entity_form_object->setEntity($webform_submission);

    // Initialize the form state.
    $form_state = new FormState();
    $form_state->setFormObject($entity_form_object);

    // Build the form.
    $entity_form = [];
    $entity_form = $entity_form_object->buildForm($entity_form, $form_state);

    // Set values
    $form_state->setValue('email', 'invalid');

    // Validate the form.
    /** @var \Drupal\Core\Form\FormValidatorInterface $form_validator */
    $form_validator = \Drupal::service('form_validator');
    $form_validator->validateForm($entity_form['#form_id'], $entity_form, $form_state);

    // Get the form state errors.
    print_r($form_state->getErrors());
jrockowitz’s picture

I found this helper to programmatically submit a form, but it does not work for entity forms, which requires some addition information and dependancies.

Nylsoo’s picture

Hi Jacob,
Thanks for the fast response! Combining the Helper and your code i got my code working.

    $webform_submission = \Drupal\webform\Entity\WebformSubmission::create([
      'webform_id' => $form_id,
    ]);
    // Get the form object.
    $entity_form_object = \Drupal::entityTypeManager()
                                 ->getFormObject('webform_submission', 'default');
    $entity_form_object->setEntity($webform_submission);

    // Initialize the form state.
    $form_state = (new FormState())->setValues($values);
    \Drupal::formBuilder()->submitForm($entity_form_object, $form_state);

    $errors = $form_state->getErrors();
Nylsoo’s picture

Status: Active » Closed (works as designed)
jrockowitz’s picture

@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

Nylsoo’s picture

@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

imclean’s picture

This 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:

/admin/structure/webform/manage/{webform}/submission/{webform_submission}

Being able to POST to /form/{webform} would be fantastic. (Incidentally, at /admin/config/services/rest it's still listed as /webform/{webform}.

imclean’s picture

My 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.

imclean’s picture

I'm having a go at wrapping #4 inside a REST resource in a custom module.

imclean’s picture

Here 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.

imclean’s picture

I'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's target_id to load the webform: GET /webform_rest/elements/webform_id?_format=hal_json.