I have created a Config Entity, one of the values it stores is the results from a set of checkboxes. This is the element in the form

$form['choices'] = [
  '#type' => 'checkboxes',
  '#title' => $this->t('Choices'),
  '#default_value' => $this->entity->getChoices() ?: [],
  '#options' => $this->choicesManager->getAsOptions(),

In the choices.schema.yml I have this:

  type: sequence
    type: string

And it works EXCEPT I always get an mbstring warning when saving the entity:

Warning: mb_strlen() expects parameter 1 to be string, array given in Drupal\Core\Form\FormValidator->performRequiredValidation() (line 333 of /shore_site/web/core/lib/Drupal/Core/Form/FormValidator.php).

Because it's a warning, it disappears after a moment (but you can see, for a moment, that the array in question contains the selected items from this element). And the config entity has been saved correctly:

  choice_1: choice_1
  choice_2: choice_2
  choice_3: '0'
  choice_4: '0'

There's more to the config entity, of course, and by removing this item the error goes away. So it's definitely this.

I have other config files where I've saved checkboxes painlessly, but they haven't been config entities. (And I have other config entities that work fine, but aren't trying to save checkboxes.)

I've tried various different ways of defining this in the schema (including using the "undefined" type) but the warning keeps coming up.

(I've looked at other answers, only one talks about the same problem and that person ended up serialising the checkbox data. I don't want to do that.)

I'm tearing my hair out - can you help me save my hair?

  • Did you try and debug line 333 of core/lib/Drupal/Core/Form/FormValidator.php? At first sight, it doesn't seem to be related in any kind to config entity storage? And it wouldn't even be triggered without the '#maxlength' property on your form element, which your example's checkboxes doesn't have... – Mario Steinitz Feb 25 at 15:00
  • To make clearer what @MarioSteinitz is saying, line 333 contains the following code: if (isset($elements['#maxlength']) && mb_strlen($elements['#value']) > $elements['#maxlength']) { /* ... */ }. – kiamlaluno Feb 25 at 16:26
  • Turns out I am an idiot. When I was tidying up the form element to show here, I removed the "#maxlength" property. Why did it have a "#maxlength" property? Because I'd copy/pasted from somewhere else. [sigh] Thanks, guys. – Adaddinsane Feb 26 at 10:41
  • 2
    @Adaddinsane, glad you found the cause for your warning. Side-note: I doubt, this question will be of great use for other Drupal Answers users. It might just pop up as false positive when someone really is looking for configuration schema issues. Consider deleting it. – Mario Steinitz Feb 26 at 13:06

I had the same issue:

In the save method of your form, edit your entity as follow before save it :

$this->entity->set('choices', array_values( array_filter( $form_state->getValue('choices') ) ) );

array_filter will filter uncheck options, array_values will extract the values.


Don't put a "#maxlength" property in a "checkboxes" element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.