Values are lost when programatically creating a submission

Created on 22 August 2024, about 1 year ago

Problem/Motivation

I followed the instructions for programatically creating a submission β†’ , but I found an issue with a required checkbox element:

privacy_verklaring:
    '#type': checkbox
    '#title': 'Ik geef toestemming om mijn gegevens te bewaren'
    '#description': '<p>Lees de <a href="/node/6" target="_blank" data-entity-type="node" data-entity-uuid="98103e08-af47-42da-a196-5089e0491ec9" data-entity-substitution="canonical">privacyverklaring</a></p>'
    '#required': true
    '#access_create_roles':
      - authenticated
      - anonymous
    '#access_update_roles':
      - authenticated
      - anonymous
    '#access_view_roles':
      - authenticated
      - anonymous

I submit the form with value "1" for key privacy_verklaring, but form validation complains that privacy_verklaring is required.

Currently, form values are copied from the form array to the FormState object one by one in the FormBuilder::handleInputElement() method, but for some reason the value for privacy_verklaring is missing at that point, while the other values are not, including another (not required) checkbox element.

Proposed resolution

Calling $form_state->setValues($webform_submission->getData()); on the newly created FormState object seems to fix the issue. By doing this, we make sure the form values are available throughout the whole form building process, and that seems to fix the issue.

πŸ› Bug report
Status

Active

Version

6.2

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

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

Merge Requests

Comments & Activities

  • Issue created by @dieterholvoet
  • Merge request !508Set form values from submission data β†’ (Open) created by dieterholvoet
  • Status changed to Needs review about 1 year ago
  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels
  • Pipeline finished with Failed
    about 1 year ago
    Total: 2168s
    #261561
  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    Did you check where the other values get set? It seems odd that some get set and some do not. Perhaps there is other code that could be fixed or that could be removed due to this change.

    Please rebase onto 6.3.x.

  • Pipeline finished with Failed
    about 1 year ago
    #262775
  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    Did you check where the other values get set? It seems odd that some get set and some do not.

    It seems like some parts of the code such as validation handlers just don't have access to the values. If there's no validation standing in the way, these values are stored just fine. Now I'm encountering the same or at least a similar issue with file uploads.

    Submitting fid's to a multiple file element works when formatted this way, values are stored correctly:

    -----------------------------417014635139932088931531491267
    Content-Disposition: form-data; name="documents[]"
    
    98
    -----------------------------417014635139932088931531491267
    Content-Disposition: form-data; name="documents[]"
    
    97
    -----------------------------417014635139932088931531491267
    

    But once you make the element required, it'll start complaining about missing values, blocking submission. The only way I have found to submit a file to a required file element, is if it only accepts a single value and if it's formatted this way:

    -----------------------------417014635139932088931531491267
    Content-Disposition: form-data; name="documents[fids]"
    
    98
    -----------------------------417014635139932088931531491267
    

    To give some context: I'm encountering this issue while working on the Webform Headless module β†’ . There's a /webform/{webform}/json/submission API endpoint that allows you to create submissions, returning any validation errors in a structured JSON format. The Drupal\webform_headless\Submitter::createSubmission() method is for the biggest part copied from the Drupal\webform\WebformSubmissionForm::submitWebformSubmission() method, with the workaround from the MR in this issue added. That workaround doesn't seem to fix the issue (completely) though.

  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    In case of the file upload, the method that doesn't have access to the value and which is setting the error is Drupal\file\Element\ManagedFile::validateManagedFile().

  • Pipeline finished with Success
    12 months ago
    Total: 187s
    #293945
  • Pipeline finished with Success
    12 months ago
    Total: 202s
    #294314
  • Pipeline finished with Failed
    9 months ago
    Total: 779s
    #388265
  • Pipeline finished with Skipped
    6 months ago
    #446780
  • Pipeline finished with Success
    4 months ago
    Total: 406s
    #497042
  • Status changed to Postponed: needs info 13 days ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    We need to document the issue with some example code with an example webform that attempts to programmatically create webform submission and fails using something like \Drupal\webform\WebformSubmissionForm::submitFormValues

Production build 0.71.5 2024