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.

CommentFileSizeAuthor
#6 chosen3457102.patch830 bytesmatslats

Comments

matslats created an issue. See original summary.

nagy.balint’s picture

Status: Active » Postponed (maintainer needs more info)

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;
      }
matslats’s picture

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.

nagy.balint’s picture

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

matslats’s picture

Category: Task » Bug report
Status: Postponed (maintainer needs more info) » Fixed

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'])) {

matslats’s picture

Status: Fixed » Needs review
StatusFileSize
new830 bytes

And here's the patch

vensires’s picture

Status: Needs review » Reviewed & tested by the community

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

nagy.balint’s picture

Status: Reviewed & tested by the community » Needs work

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.

  • nagy.balint committed 2e0134e6 on 5.0.x
    feat: #3457102 Field cardinality not respected
    
    By: matslats
    By: nagy....
nagy.balint’s picture

Status: Needs work » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.