Field cardinality not respected

Created on 25 June 2024, 10 months ago

I'm posting this for 4.x although there are reports dating back 10 years ago for older branches.
My field has a cardinality of 3, but Chosen allows the user to select any number of items and then the form fails validation.

📌 Task
Status

Active

Version

4.0

Component

Documentation

Created by

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

Comments & Activities

  • Issue created by @matslats
  • Status changed to Postponed: needs info 8 months ago
  • 🇭🇺Hungary nagy.balint

    Hi!

    Can you provide more details?

    For me at least on 4.0.x if the cardinality is 5 let's say, then I cannot select more than 5 items, the dropdown does not come up.

    And it is already using the "max_selected_options" of the chosen library.

          var cardinality;
          if ($element.attr('multiple') && (cardinality = $element.data('cardinality'))) {
            options.max_selected_options = cardinality;
          }
    
  • Thanks for asking.
    My field is actually a basefield, not a field API field. I don't know if that makes a difference.

        $fields['categories'] = BaseFieldDefinition::create('entity_reference')
          ->setLabel(t('Category'))
          ->setDescription(t('The category or categories of the ad.'))
          ->setSetting('target_type', 'taxonomy_term')
          ->setSetting('handler', 'default:taxonomy_term')
          ->setSetting('handler_settings', ['target_bundles' => ['categories']])
          ->setRequired(TRUE)
          ->setCardinality(3)
          ->setTranslatable(TRUE)
          ->setDisplayConfigurable('view', TRUE);
    

    That's all the extra information I can think of right now.

  • 🇭🇺Hungary nagy.balint

    Maybe in that case it will miss the required attribute and data, which is there on field API fields.

  • Status changed to Fixed 5 months ago
  • On src/ChosenFormRender.php line 73, the #bundle property is required but base fields have no bundle.
    if (isset($element['#entity_type']) && isset($element['#bundle']) && isset($element['#field_name'])) {
    However, inside that if block, the case of no bundles is handled

      $field_config = FieldConfig::loadByName($element['#entity_type'], $element['#bundle'], $element['#field_name']);
            if ($field_config) {
                $field = $field_config->getFieldStorageDefinition();
            }
            else {
               \\ cardinality is taken from the basefield definition
            }
    

    So it looks like a simple bug.
    Replace
    if (isset($element['#entity_type']) && isset($element['#bundle']) && isset($element['#field_name'])) {
    with
    if (isset($element['#entity_type']) && isset($element['#field_name'])) {

    if (isset($element['#entity_type']) && isset($element['#field_name'])) {

    The problem was not because it was a base field but because my entity

  • 🇬🇷Greece vensires

    It's a really small change and seems to fix the issue correctly. Setting as RTBC.

  • 🇭🇺Hungary nagy.balint

    The patch seems to be wrong here, since

    FieldConfig::loadByName($element['#entity_type'], $element['#bundle'], $element['#field_name']);
    still uses bundle, maybe something is missing from the patch.

Production build 0.71.5 2024