Setting the integer zero (0) to a required numeric field, when performing a webform submission, results in a "field is required" error

Created on 8 June 2023, over 1 year ago
Updated 9 December 2023, about 1 year ago

Problem/Motivation

If you have a required "Number" field in your webform, when you make a submission using the webform_rest_submit resource (/webform_rest/submit) and pass the integer 0 as the value of that field in the payload, you will get a "field is required" error.
Actually zero (0) should be an allowed value, even for required numeric fields. It works if you input a zero in the numeric field when submitting the webform from the Drupal interface (the numeric inputted value is formatted as a string ("0") at some point of the form submission process, what is interpreted as a valid non-empty value, see below). But, when using the REST resource and an integer (0) or double (0.0), it fails.

The problem occurs because the following evaluation made in WebformSubmissionConditionsValidator::validateFormElement() only admits the string "0" as a non-empty value, whereas any other format for a number (integer, double) will not pass the empty() validation, then WebformElementHelper::setRequiredError() will be called.

$is_empty = (empty($value) && $value !== '0');
$is_default_input_mask = (TextBase::isDefaultInputMask($element, $value));

// If required and empty then set required error.
if ($is_empty || $is_default_input_mask) {
  WebformElementHelper::setRequiredError($element, $form_state);
}

See: https://git.drupalcode.org/project/webform/-/blob/6.1.4/src/WebformSubmi...

Of course I do not want to force all the API consumers to always format these numbers as a string. The handling must be in the backend.

Steps to reproduce

Make a POST request to the endpoint /webform_rest/submit setting the integer zero to a numeric field in the payload:

{
  "webform_id": "your_webform_id",
  "some_text_field": "Lorem Ipsum",
  "some_number_field": 0,
}

Result:

{
    "message": "Submitted Data contains validation errors.",
    "error": {
        "some_number_field": "Your ABC field is required."
    }
}

Proposed resolution

There could be 3 options:

  • Check the submitted values for the webform "Number" fields in the data received in Drupal\webform_rest\Plugin\rest\resource\WebformSubmitResource::post(), before the data is sent to validation. If an empty numeric value is detected, convert the value to a string
  • Change the logic in the method WebformSubmissionConditionsValidator::validateFormElement() in the Webform module. But I think it's serving its overall purpose well. Not only number fields are being evaluated there and number fields submitted from the Drupal webform UI are not falling into this issue
  • Force all API consumers to format the numbers as strings
🐛 Bug report
Status

Postponed: needs info

Version

4.0

Component

Code

Created by

🇨🇴Colombia camilo.escobar

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

Comments & Activities

Production build 0.71.5 2024