Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
For fields with options widgets, the submitted value for the empty option is actually '_none
'. In options_field_widget_validate()
, required fields have custom validation.
This bug may occur with other widget types that alter value during validation. Was there a reason why the attached field forms are validated before checking for empty value, as opposed to after? EDIT: Never mind.
Comment | File | Size | Author |
---|---|---|---|
#3 | 1676206-03-validate-empty-option.patch | 1.12 KB | zhangtaihao |
Comments
Comment #1
zhangtaihao CreditAttribution: zhangtaihao commentedIn these widgets, element validators alter and check value. In
field_collection_field_widget_embed_validate()
,field_attach_form_validate()
relies on form values having been altered.So, there seems to be no way to check if a field collection is empty without having run element validators. Validation checks sensitive to #required are completely bypassed.
Comment #2
zhangtaihao CreditAttribution: zhangtaihao commentedMind you:
#1585930: Options module uses '_none' as a special value
Comment #3
zhangtaihao CreditAttribution: zhangtaihao commentedWould it be worth making a special case for '_none'?
Comment #4
zhangtaihao CreditAttribution: zhangtaihao commentedChanging status.
Comment #5
jitse CreditAttribution: jitse commentedI applied the patch on dev and it's working as expected.
In my case it's about required field_collection taxonomy fields on a node with a singular field_collection.
Without the patch no validation is done, after the patch the "field is required" message is shown on the form.
As far is i can tell the patch is coded by drupal standards, it does not involve unittests and it fixes the described behaviour.
Comment #6
fagoThere should be a "#empty_option" form api key - so we should check for this one.
Also, the comment should not link this issue. We do not provide a history of changes as comments ;)
Comment #7
zhangtaihao CreditAttribution: zhangtaihao commentedUm... it's not there (apart from a
#properties
property). The Options module seems obsessed with the '_none
' value without actually using it as#empty_value
.Do you reckon then this is a core bug? I seem to remember there being specific reasons '
_none
' is specially handled (e.g. #735426: Fields with select widget: first option is selected by default even if no 'default value' set for the field).Comment #8
merlinofchaos CreditAttribution: merlinofchaos commented#7 is correct. options.module is not doing a smart thing, but that can't be helped. The patch in #3 solves the issue as well as possible.
Comment #9
briand44 CreditAttribution: briand44 commentedPatch in #3 worked for me.
Comment #10
briand44 CreditAttribution: briand44 commentedJust found this related issue: Required fields in collection not validated when empty. The patch here seems to solve this issue as well and it also fixed a similar problem for me with a date field not validating. Not sure if this issue should be marked as a duplicate?
Comment #11
fagoI see - thanks for clarifying. Committed.
Comment #12.0
(not verified) CreditAttribution: commentedRemoved question.
Comment #13
TBarina CreditAttribution: TBarina commentedHi all. Thanks for the patch. There is a further problem though because the code of the module is not compatible with Conditional Fields module (as far as fields with options widgets are concerned, at least, but probably for other types of fields as well).
In my case I have a custom content type (named "press-review") which contains, among others, a Field collection set with an unlimited number of values.
Such Field collection, in turn, has the following fields:
- title
- body
- date
- doc_type (required field) (which is a select option with the following possible values: 1|article, 2|note, 3|press_release)
- headline (required field)
I'm using the Conditional Fields module (version = "7.x-3.0-alpha1+12-dev").
I set a dependency in my Field collection according to which headline should only be visibile in case doc_type value is set to 1 or 2.
I'm facing the following problem: I create a new press-release content type and I fill in the first field collection item (let's say an article from NY Times) and then I add a new field collection item for entering a second element: a press-release.
I select the relevant doc_type: 3|press_release and the Conditional fields dependency makes headline hidden in the form. After filling in the title and the body fields, I try and save the content type but I get an error message stating that headline is a required field!
By debugging the code I noticed that, at the time those lines of code are executed, $elements['#required'] of my second headline item in the field collection is set to False (which is correct because that has become an hidden field; $elements['#required'] is probably reset to False by Drupal core during standard validation process).
Therefore I would propose to change the patch slightly by adding a check concerning the #required attribute like this:
// Copied from _form_validate().
+ // #1676206: Modified to support options widget.
if (isset($elements['#needs_validation']) && $elements['#required'] == true) {
$is_empty_multiple = (!count($elements['#value']));
...
Please, let me know.
Many thanks in advance.
Comment #14
TBarina CreditAttribution: TBarina commentedAny answer?