Problem/Motivation

One can make a subfield required by creating a validator and adding it with hook_form but the error message can only be displayed for the entire custom field and not for the specific sub field. A more user friendly approach would be to add the ability to mark a subfield as required and have the subfield display as invalid (red).

Steps to reproduce

Proposed resolution

Add fields to subfield configuration to make them required and to add a validation error message.

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

schiavone created an issue. See original summary.

dave reid’s picture

It would be nice to have this to help also define "what minimum subfields are required for an item to be empty or not empty". If I create a custom field for a link collection with image + link + description, the description might be optional, but the image and link are required to save that field delta. We are having to write a lot of custom field validation and altering to make this work, and it would be lovely to have this supported upstream.

It gets complicated of course when you have multiple value custom field, and you cannot make those subfields actually required (you can make them just visually marked as required but not validate as required), otherwise you can't submit the form with an empty second field value.

apmsooner’s picture

From a saved data standpoint, each subfield has the setting "Discard other values if this field is empty" which means you can control what is considered empty in whole if any of those subfields marked with that are empty.

From a validation standpoint, the 'required' for each subfield was purposefully reliant on whether the whole field is required. Particularly for multi-valued as you noted, it gets really complex and messy otherwise. Open to ideas....

apmsooner’s picture

Status: Active » Postponed (maintainer needs more info)

After re-reading the request, it sounds like you might be requesting something that already exists? Every sub-field already has a required checkbox but those fields are only visible when the actual custom field itself is marked required. I'm wondering if you just didn't mark the required box for the actual field and thus you aren't seeing those sub-field required checkboxes. BTW, translations work the exact same way if you're doing anything with multilingual. The sub-field settings are dependent on the field setting in both cases. Therefore, there is no need for custom validation as you stated, it should just work.

schiavone’s picture

Wow. Thanks for explaining. Perhaps there could be some help text on the parent field required checkbox.

apmsooner’s picture

Title: Add ability to make subfield required » Make sub-field required visible but enabled dependent on field setting
Component: Code » User interface
Status: Postponed (maintainer needs more info) » Needs review

Since the help text on the sub-field required already notes applicability, how about we just change state from 'visible' to 'enabled'? See patch.

apmsooner’s picture

Status: Needs review » Fixed

The original issue was deemed one of not seeing the sub-field required checkbox due to visibility state reliant on parent field setting, therefore I've changed to state to 'enabled' to satisfy the confusion. Anything else related to states api as mentioned in review I am going to assume is unrelated issue. Considering this fixed and merged.

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.

apmsooner’s picture

Title: Make sub-field required visible but enabled dependent on field setting » Make sub-field required 'visible' but 'disabled' dependent on field setting

Status: Fixed » Closed (fixed)

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