Empty collections will not validate on required fields

Created on 3 November 2020, about 4 years ago
Updated 29 January 2023, almost 2 years ago

Problem/Motivation

When having, for example multiple textfields, in a single collection which are all / partly required, and is submitted with none of them filled the module will not validate the form for the any of the required fields in the collection.

Steps to reproduce

  1. Create a field collection with, say 5 textfields, all required.
  2. Submit a form with this collection, leave all fields empty.
  3. Note that the form will never complain about any of the fields being empty.
  4. Try re-submitting the form, but now fill one of the 5 fields.
  5. Note that now the form will actually validate the requirements and give errors.

Proposed resolution

Leave out the !field_collection_item_is_empty($field_collection_item) from the if statement on field_collection.module:1775.

Remaining tasks

I'll submit a patch.

User interface changes

None

API changes

None

Data model changes

None

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇳🇱Netherlands aken.niels@gmail.com

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇧🇾Belarus drupalprojects

    Guys, the fix doesn't work properly. I've installed latest 7.x-1.x-dev version and one of my users complained that he cannot save a form anymore. The form has a field_collection item (non-mandatory itself) with 2 mandatory fields inside the collection.
    I assume that if a field collection is not marked as mandatory - I should be able to save the form without adding any values to it. So with this patch applied - it didn't work properly and it took me years to find a root cause for that.
    Once I added the check for if (!empty($element['#field_collection_required_elements']) && !field_collection_item_is_empty($field_collection_item)) on line 1788 of field_collection.module - it is working properly now.

    By the way, I have another field_collection item on the same node which is mandatory and with my fix it is still validating that other mandatory field properly.

  • Status changed to Needs review almost 2 years ago
  • 🇪🇸Spain trebormc Barcelona

    #9 works well for me.
    My client had the same problem as mentioned in #8.

  • 🇭🇺Hungary nagy.balint

    Works for me as well.

  • 🇪🇪Estonia ram4nd Tallinn
Production build 0.71.5 2024