Add save as draft feature

Created on 10 January 2020, almost 5 years ago
Updated 4 June 2023, over 1 year ago

I am trying to figure out how to POST a submission as a draft and skip the validation on the backend. Essentially replicating the current draft functionality that Webforms supports in Drupal.

Feature request
Status

Fixed

Component

Code

Created by

🇺🇸United States brado

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇵🇹Portugal nsalves

    Fixed on the 4.0.2 release

  • Status changed to Fixed over 1 year ago
  • 🇮🇹Italy Giuseppe87

    Is this feature ready? I'm trying to use it at doesn't work.
    I'm using Webform 6.1.4 and Webform REST 4.0.3.

    I think I found at least 3 problem, if my flow is correct.

    I'm using a webform with two required field, e.g. test and test_2
    E.g. I do a POST against /webform_rest/submit with

    {
        "webform_id": "my_webform",
        "test": "test",
        "draft":  true
    }
    

    1. The first POST is done correctly. Afterwards, if I try to POST

    {
        "webform_id": "circularity_tool",
        "test": "tes",
        "test_2": "test 2",
        "draft":  0
    }

    I get the error "the field test_2 is required".

    This probably because inside WebformSubmissionForm::submitWebformSubmission()

       // Submit the form.
        \Drupal::formBuilder()->submitForm($form_object, $form_state);
    
        // Get the errors but skip drafts.
        $errors = ($webform_submission->isDraft() && !$validate_only) ? [] : $form_state->getErrors();
    

    The first line returns a previous state for $form_state - e.g. the data of the first post - while the webform_submission is correctly not a draft. This gives the error.

    I'm thinking the check on the draft is wrong and should be fix (webform issue in case) into something like

       // Get the errors but skip drafts.
        $errors = ($webform_submission->isDraft()) ? [] : $form_state->getErrors();
    

    But that still doesn't fix the error, as it just skip the check for "draft webform" while leaving open for "not draft one".

    2. The PATCH of a submission currently doesn't support the draft. If I had a code similar to the POST one:

        if (!empty($webform_submission)) {
          //Check if webform allows drafts
          $allow_draft = $webform_submission->getWebform()->getSetting('draft');
    
          if(isset($webform_data['draft']) && $allow_draft === 'none' && $webform_data['draft'] === TRUE){
            $errors = [
              'error' => [
                'message' => $this->t('This webform does not allow draft submissions.'),
              ],
            ];
            return new ModifiedResourceResponse($errors, 400);
          }
    
          if (isset($webform_data['draft'])) {
            $in_draft = $webform_data['draft'] !== TRUE ? FALSE : TRUE;
            $webform_submission->set('in_draft', $in_draft);
            unset($webform_data['draft']);
          }
    [...]
    

    I get a problem whose cause is probably as well inside submitWebformSubmission().
    With the $errors = ($webform_submission->isDraft()) ? [] : $form_state->getErrors(); of point 1, as long as I PATCH webform keeping them in draft, it works.

    E.g. body with

    {
        "test": "new value",
        "test_2": null,
        "draft": true
    }
    

    But the moment I put the draft to false, the PATCH fails because

    $errors = WebformSubmissionForm::validateWebformSubmission($webform_submission); does work correctly

    but the following actual save

    $webform_submission = WebformSubmissionForm::submitWebformSubmission($webform_submission);

    fails as the Drupal::formBuilder()->submitForm($form_object, $form_state); makes for some reason the $form_data empty!

    Am I missing something? Is actually this not working?

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed over 1 year ago
  • 🇮🇹Italy Giuseppe87

    The issue has been automatically closed, yet i fear my questions above stand open.

Production build 0.71.5 2024