ConfigFormBase produces an error when saving empty number value from the form to a numeric schema item: This value should be of the correct primitive type

Created on 1 May 2025, about 1 month ago

Problem/Motivation

I have a number type in my config schema, that is not required, so can be empty. And created a config form to manage the value. It works well if I fill the number value, but doesn't work if I keep the value empty, producing an error:
This value should be of the correct primitive type.

Steps to reproduce

1. Create a schema: my_module.schema.yml:

my_module.my_config:
  type: config_object
  label: 'My Config'
  mapping:
    my_int:
      type: integer
      label: 'My int'

2. Create a config form to manage the value - Form/MyConfigForm.php:

namespace Drupal\my_module\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class MyConfigForm extends ConfigFormBase {
  public function getFormId() {
    return 'my_config_form';
  }
  public function getEditableConfigNames() {
    return ['my_module.my_config'];
  }
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['my_int'] = [
      '#type' => 'number',
      '#title' => $this->t('My Int'),
      '#config_target' => 'my_module.my_config:my_int',
    ];
    return parent::buildForm($form, $form_state);
  }
}

3. Open the form page, type any number into the field value, submit - the config will be updated successfully.
4. Make the field value empty, submit - the form will produce the validation error.

Proposed resolution

Seems this issue is related to the validation of the individual configuration values, and casting '' (empty string) to NULL.

I found a pretty old post in the issue https://www.drupal.org/project/drupal/issues/2130811#comment-8280187 β†’ related to this, and in this issue the problem with casting '' to NULL was resolved even for 8.0.x-dev, but seems not fully, or there is a regression.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component

configuration system

Created by

πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024