I have a content type with a Parent Entity Reference field and (3) Child Dependent fields. All are multi-value, select list fields.

When I create a new node:

1. None of the child field values are saved. I have to go back in and edit the node and then re-save...this saves the child field values.
2. On node creation, the child fields are single value select only (even though fields are multi-value). After I save and then edit, I can make a multi-value selection
3. If I change the Parent field value when editing the node, I get an error message ("An illegal choice has been detected. Please contact the site administrator.") and the child values are not saved. I then have to re-edit without changing the parent, select child values, and save. This saves the child values.

Sorry to include multiple problems in one post...but it seems they all might be from the same root cause.

Thanks!

Comments

zdean created an issue. See original summary.

zdean’s picture

Priority: Normal » Major
Mirto’s picture

I have the same problem too. Any suggestions?

electrokate’s picture

I struggled with this alot https://www.drupal.org/project/ddf/issues/2851762#comment-12994315 and I ended up having to write custom code for dependent fields. The Business Rules module tries to modify the form on ajax call which violates Drupal validation. That's why it will only work when replacing one option, as you can't tell a form there's only one option and then on ajax try to say it has several.

c.e.a’s picture

Can you please give a detailed steps on how to reproduce the error ?
thank you

zdean’s picture

I included the steps in the original post. Is there something I left out that would be helpful with this issue?

c.e.a’s picture

The question mentioned using the 8.x-1.0-beta4 version of the module... can you confirm/test again with the last dev release ?

zdean’s picture

With latest dev, I'm unable to create a multi-value child field.

1. I create a single value entity reference parent field (drop-down)
2. I create an entity reference view
3. I create a multi-value child field using the above entity reference view as the filter and the above parent field as the parent

If I create the child as a drop-down list, I can select a single child value once I make a parent selection. If I create the child as a checkbox, the child field does not show any values regardless of my selection for the parent field.

Therefore not able to test this issue against dev.

c.e.a’s picture

I will have to escalate this issue to the maintainer for further digging into finding the problem...
the issue is added to #3041221: List of all 'Bug reports' that need immediate support Assigned to: yseki

zdean’s picture

For added context, this issue seems (maybe??) related to #3013707: Parent with Multiple Children - Requires Select List for all fields.

1. Child field values only show up on node creation if Parent and all Child fields are drop-down (none of the fields can be checkbox...if checkbox is used for any of the fields, the child will not show any values)
2. On node creation, Child field value is not saved when saving the node. Node must be edited then resaved for the child values to be saved.
3. If parent value is changed on edit of node, you get an error message ("An illegal choice has been detected. Please contact the site administrator.") and child values aren't saved

So in summary:

1. Checkbox widget doesn't work as per #3013707: Parent with Multiple Children - Requires Select List for all fields and
2. Child values are not saved on node creation and
3. Changing parent field value on edit causes error and doesn't save child values

c.e.a’s picture

Version: 8.x-1.0-beta4 » 8.x-1.x-dev
Priority: Major » Critical
c.e.a’s picture

Assigned: Unassigned » yseki
kris77’s picture

I'm trying to solve the problem because it's a function that I need for my project in drupal8...

pieterjandp’s picture

Status: Active » Needs review
StatusFileSize
new629 bytes

The child fields are single value select fields because they have no options when they are created, which causes the select field to be a single value select field (see https://github.com/drupal/drupal/blob/8.8.x/core/lib/Drupal/Core/Field/P...). This causes the form data to contain an incorrect field name (without [] at the end), which causes the data not to be saved correctly.

The attached patch uses business_rules_field_widget_form_alter to change these child fields to multiple value fields. It should solve the first two problems described in this issue. I did not run into the third problem myself, so I can't verify if this third problem is linked to the others.

pieterjandp’s picture

StatusFileSize
new742 bytes

I added a comment with some context to the patch.

pieterjandp’s picture

StatusFileSize
new808 bytes

Sorry, I found a bug in my patch. It threw an error when the $current_field is not an index in the field definitions array.

kris77’s picture

@pieterjandp i have tried your patch, and it same to work only for "select list widget" and not for "checkboxes widget".

Thank you so much for your work.

pieterjandp’s picture

StatusFileSize
new851 bytes

The new patch doesn't break forms with the "checkboxes widget" any more.

I did not manage to get business rules dependent field working with the "checkboxes widget", so I can't test if there are issues with this widget / this patch fixes them. I get the following JavaScript error:

Uncaught TypeError: Cannot set property 'length' of undefined
    at Drupal.AjaxCommands.updateOptionsCommand
kris77’s picture

@pieterjandp now there are no errors, but the field remains empty.

colan’s picture

Title: Dependent Child fields are not saved » Multi-valued dependent child fields are not saved

Let's be more specific in the title.

colan’s picture

Assigned: yseki » Unassigned
Priority: Critical » Normal
Status: Needs review » Needs work

Downgrading priority as the module still works without using multi-valued dependent fields, and updating status as per the error in #18.

pieterjandp’s picture

Warning: the patches in this issue should be applied after the patch https://www.drupal.org/files/issues/2019-05-28/paragraphs-3040253-7.patch (see #3040253: Add AJAX support to Paragraphs)

I created a new patch, which adds support for "checkboxes widget".

I discovered a new issue when some content is edited, and a parent field and subsequently a dependent child field are changed. I this case, an illegal choice error will logged when changing the parent field. This error will also be displayed to the end user when the content is saved, although the content is saved successfully. This is because the form is validated during the AJAX call which updates the dependent child field. During this call, the parent field has a new value, while the child field still has the old value, which results in an invalid combination. I believe this issue is related to #3051993: Illegal choice error when parent field is changed and child is not populated, so I think this can better be followed up there.

kris77’s picture

@pieterjandp your patch works like a charm with "checkboxes widget" now.

Thanks a lot for your work.

I discovered another issue when content is edited in my case. This is my scenario:

In my content type I have 4 fields. All are made with business rules dependent field, and the last field is unlimited with "checkboxes widget" because user can select one or more option.

For the last field I have a view that have a contextual filter and a relation with another table too, because the values to be returned depend on the relationship.

So, when create my node, the last field is filled like a charm with patch in #22. After save the node, click on edit and the last field is filled with ID value of contextual filter and not with the name of the field in relationship.

I don't know if it is right to create another issue or it is solved here.

colan’s picture

@pieterjandp: It would be nice if you could edit your above comments and replace the issue links with the drupal.org markup that shows the issue title and status (colour). Use the format "[ # 999999 ]" (without quotes or spaces). Thanks!

pieterjandp’s picture

StatusFileSize
new8.62 KB

Small improvement: no longer remove the _none option in a select list, even if the field is required. If a user creates new content, and the _none option is removed, the user might otherwise end up with the first other option without realizing.

@Kris77 I think it's best to open a new issue for the contextual filter problems and reference this issue in the new issue.
@colan Thanks, updated.

toprak’s picture

Tested. Couldn't apply patch-25

colan’s picture

Component: Code » Dependent fields
johannes-maximilian’s picture

I'm not sure if this might be helpful for others but for my needs the conditions to get the cardinality of the field was to strickt, because…getCardinality() === -1 would only affect fields that have an unlimited number of allowed values. According to this, I changed the condition to check against a cardinality which is not 1, to affect fields that allow more than one value.

      // Make sure multi value dependent fields stay multi value, even if they don't have any options initially
      if (isset($entity->getFieldDefinitions()[$current_field]) &&
        $entity->getFieldDefinitions()[$current_field]->getFieldStorageDefinition()->getCardinality() !== 1 &&
        $entity->getFieldDefinitions()[$current_field]->getSetting('handler') === 'business_rules_views' &&
        $element['#type'] === 'select' &&
        !$element['#multiple']
      ) {
        $element['#multiple'] = TRUE;
      }
el1_1el’s picture

rerolled 25 for beta10. also included a combined patch with Add AJAX support to Paragraphs
since this one needs that one as stated in 22

lubwn’s picture

Confirming that Combined patch from #29 works like a charm for me, even with checkboxes! But it needs to be applied to beta10, not -dev branch, because it fails to apply to -dev branch.
Only thing I came across is an error message when editing already saved node with dependent fields, and changing parent and child in one saving. But it does the job anyway, even when it prints an error.
Thank you very much!

kris77’s picture

As well as @lubwn, confirm that patch 3014031-combined.patch from #29 works like a charm for me too.

I applied the patch to beta10 with no problems.

Thanks a lot @el1_1el

shenron_segamag’s picture

I can confirm it works well with beta10 (not tested with beta11, since it broke dependent fields).

tim-diels’s picture

StatusFileSize
new15.29 KB
new610 bytes

When using select2, this won't work anymore as the type is specific for select. So added select2 as type for the combined patch as I could not get the normal patch to apply.

niral098’s picture

#29 combined patch for beta10 version works. In order to avoid error message ( "Found an incorrect selection. Please contact your site administrator"), I have defined #limit_validation_errors property to the specific field that trigger the ajax call and hence the form validation inside form alter -
$form['field_name']['widget']['#ajax']['#limit_validation_errors'] = array();

kris77’s picture

This is broken again in 2.0.0-beta1.

Multi-value and checkboxes widget does not work.

el1_1el’s picture

Version: 8.x-1.x-dev » 2.x-dev
Status: Needs review » Needs work
Related issues: +#3190509: Dependent Field is broken in beta11
StatusFileSize
new10.56 KB

Here's a patch for 2.0beta1 and current dev. Note that this replaces the js in https://www.drupal.org/project/business_rules/issues/3190509 with what was working for me in beta10.

el1_1el’s picture

wrong patch in 36. ignore it

el1_1el’s picture

kris77’s picture

Thank you so much @el1_1el, your patch in #37 seems to work for me.

tim-diels’s picture

I don't get why the select2 is removed? So i re-added it

tim-diels’s picture

StatusFileSize
new12.72 KB

Small copy paste error

rcodina’s picture

I have tried patches on #37 and #41.

1) Both patches work fine with select fields. However, if you edit a node and you change parent and child selection, it shows an error of "An illegal choice has been detected. Please contact the site administrator." Although this error is shown, everything is working as expected!

2) Patch #41 work fine with select2 fields. I think select2 support is necessary because both #37 and #41 doesn't work with Autocomplete widgets. However, autocomplete feature can be done using select2. Using select2, the error I described before is also shown but everything is working as expected too!

Keep up the good work. Many thanks to all!

kris77’s picture

@rcodina for Autocomplete widgets you can try this patch: https://www.drupal.org/project/business_rules/issues/3207050#comment-140... and for "An illegal choice......" error try this: https://www.drupal.org/project/business_rules/issues/3051993

shenron_segamag’s picture

Hi,

The combined patch worked just fine with previous versions of Drupal 8, although it cannot be applied anymore with the latest versions.

shenron_segamag’s picture

Hi, any progress on this issue ?

rcodina’s picture

@Shenron_segamag I have this working on Drupal 9.2.13 with drupal/business_rules version 2.0.0-beta1 patched with #41. Haven't tried yet with 9.3.x. I'm also using drupal/select2 8.x-1.13 (see my previous comment).

shenron_segamag’s picture

Thanks @rcodina, Patch #41 worked just fine !

brockfanning’s picture

We are trying out patch #41 as well, and so far it seems to be working. I'll definitely follow up if any issues come up. Thank you so much!

rcodina’s picture

Status: Needs work » Reviewed & tested by the community

Two positive reviews of patch #41.

  • rcodina committed a7cb9e17 on 3.x authored by tim-diels
    Issue #3014031 by pieterjandp, el1_1el, tim-diels, Kris77, C.E.A: Multi-...
rcodina’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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