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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

zhangtaihao’s picture

In 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.

zhangtaihao’s picture

zhangtaihao’s picture

Title: Required field with options widget is not validated » Required field with options widget is not validated for empty value '_none'
FileSize
1.12 KB

Would it be worth making a special case for '_none'?

zhangtaihao’s picture

Status: Active » Needs review

Changing status.

jitse’s picture

Status: Needs review » Reviewed & tested by the community

I 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.

fago’s picture

Status: Reviewed & tested by the community » Needs work

There 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 ;)

zhangtaihao’s picture

Um... 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).

merlinofchaos’s picture

Status: Needs work » Needs review

#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.

briand44’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #3 worked for me.

briand44’s picture

Just 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?

fago’s picture

Status: Reviewed & tested by the community » Fixed

I see - thanks for clarifying. Committed.

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Removed question.

TBarina’s picture

Issue summary: View changes

Hi 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.

TBarina’s picture

Any answer?