Skip to content

Conversation

@Gytjarek
Copy link

What

Fixes duplicate file count validation errors when submitting single-file upload fields via GraphQL multipart requests. Single-file fields now correctly skip the multi-upload pipeline and only use $_FILES, preventing files from being counted twice.

Why

When submitting single-file upload fields via GraphQL multipart requests, files were being processed through both mechanisms:

  1. $_FILES - Files from multipart form data (handled by FileUploadValuesInput)
  2. gform_uploaded_files - Files processed through the multi-upload pipeline in initialize_files()

This caused Gravity Forms' get_submission_files() method to find the same file in both:

  • $files['existing'] - from GFFormsModel::$uploaded_files (populated via gform_uploaded_files)
  • $files['new'] - from $_FILES

Result: Validation errors like:

"Number of files (2) exceeds limit (1)."

Root Cause: In EntryObjectMutation::initialize_files(), the code was only skipping the multi-upload pipeline for single-file fields when $_FILES didn't exist. However, when files are submitted via GraphQL multipart, $_FILES does exist, so single-file fields would still go through the multi-upload processing, causing duplicates.

How

Modified EntryObjectMutation::initialize_files() to always skip multi-upload processing for single-file fields, regardless of $_FILES existence:

Before:

if ( ! $field->multipleFiles && ! isset( $_FILES[ $input_name ] ) ) {
    // Only skipped if $_FILES didn't exist
    continue;
}
// Single-file fields would still process through multi-upload pipeline

After:

if ( ! $field->multipleFiles ) {
    // Initialize $_FILES if needed to prevent notices
    if ( ! isset( $_FILES[ $input_name ] ) ) {
        $_FILES[ $input_name ] = [ /* ... */ ];
    }
    // Always skip multi-upload processing for single-file fields
    continue;
}

Changes:

  • Single-file fields now always skip the multi-upload pipeline
  • Single-file fields only use $_FILES (handled by FileUploadValuesInput)
  • Multi-file fields continue to use gform_uploaded_files (unchanged behavior)
  • Added inline comments explaining why single-file fields skip the pipeline

Testing Instructions

  1. Set up a Gravity Forms form with a single-file upload field (not multi-file)
  2. Submit the form via GraphQL using multipart form data with a file upload
  3. Verify the submission succeeds without validation errors
  4. Verify the file count is correctly reported as 1 (not 2)
  5. Test with a multi-file upload field to ensure it still works correctly
  6. Verify existing file upload functionality remains unchanged

Example GraphQL mutation:

mutation SubmitForm($id: ID!, $fieldValues: [FormFieldValuesInput!]!) {
  submitGfForm(input: { id: $id, fieldValues: $fieldValues }) {
    entry {
      id
    }
    errors {
      id
      message
    }
  }
}

Expected Result: No "Number of files (2) exceeds limit (1)" errors for single-file fields.

Additional Info

Error before fix:

{
  "data": {
    "submitGfForm": {
      "errors": [
        {
          "id": 10,
          "message": "Number of files (2) exceeds limit (1)."
        }
      ]
    }
  }
}

After fix: Submission succeeds with correct file count.

This fix ensures proper separation between single-file and multi-file upload handling, preventing duplicate file processing when using GraphQL multipart form data.

Checklist:

  • This PR is tested to the best of my abilities.

  • This PR follows the WordPress Coding Standards.

  • This PR has proper inline documentation.

  • This PR has unit tests to verify the code works as intended.

  • The changes in this PR have been noted in CHANGELOG.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant