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
Issue fork custom_field-3586070
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
Comment #2
dave reidIt 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.
Comment #3
apmsooner commentedFrom 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....
Comment #4
apmsooner commentedAfter 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.
Comment #5
schiavone commentedWow. Thanks for explaining. Perhaps there could be some help text on the parent field required checkbox.
Comment #7
apmsooner commentedSince the help text on the sub-field required already notes applicability, how about we just change state from 'visible' to 'enabled'? See patch.
Comment #8
apmsooner commentedThe 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.
Comment #10
apmsooner commented