Problem/Motivation
The States API won't pick up the value of a select field with #multiple = TRUE option.
Steps to reproduce:
- Create two fields in a form with the following code:
$form['dependee'] = array(
'#type' => 'select',
'#options' => array(
'a' => 'Option A',
'b' => 'Option B',
'c' => 'Option C',
),
'#multiple' => TRUE,
);
$form['dependent'] = array(
'#type' => 'textfield',
'#states' => array(
'visible' => array(
'select[name="dependee[]"]' => array('value' => array('a')),
),
),
);
The dependent field will stay hidden regardless of the value of the dependee. This happens because the value of a multiple select field is an array, and States tries to compare it with the reference with a === operator, which returns always false.
Proposed resolution
Add a handler for arrays in states.Dependent.comparisons. This works with ANDed values:
'select[name="dependee[]"]' => array('value' => array('a', 'b')),
and with ORs as well (following the syntax proposed in #735528: FAPI #states: Fix conditionals to allow OR and XOR constructions):
'select[name="dependee[]"]' => array(array('value' => array('a')), array('value' => array('c')))
Remaining tasks
- Land #2702233: [backport] Add JavaScript tests for Form API #states: required, visible, invisible, expanded, checked, unchecked so we have somewhere to put tests for this.
- Expand that test to cover this case.
- Upload test-only vs. full patch to verify test and fix.
- Further reviews/refinements.
- RTBC.
- Commit to D9/D8.
- Open follow-up issue to backport to D7.
User interface changes
This feature finally works as intended:
Before

After

API changes
N/A
Data model changes
N/A
Release notes snippet
TBD.
| Comment | File | Size | Author |
|---|---|---|---|
| #167 | 1149078-167.test-form-after-running.png | 31.11 KB | dww |
| #112 | syntax-error-unsupported-pseudo-selector.png | 348.34 KB | esod |
Issue fork drupal-1149078
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 #1
peterpoe commentedReference and values array should have the same length.
Comment #3
arcaneadam commentedI've tweaked the patch above to make for nicer code along with adding comments. I think some discussion needs to be had about how to handle multi-selects though, because in the first patch it only returns true if the two arrays match exactly. Where as my patch will return true as long as the values given in #states are selected in the select box, even if there are additional values.
So for example if my form structure looks like this
and in my select box i have "True' and 'False' both picked then my patch evaluates to true. I wonder if there doesn't need to be some sort of extra key in the state api to tell it to do exact or inclusive checking, though I think there is already another issue for that somewhere.
How it is right now I think this patch at least fixes a current problem and limitation with the states api.
Comment #4
arcaneadam commentedAlso as a side note for anyone running into this issue and not wanting to patch core. You can sort of hack around this in a custom js using something similar to the following:
Comment #5
pacufist commentedI faced this problem on Drupal 7.14.
After apply patch #3 all started work just perfect.
Comment #6
wuinfo - bill wu commentedPatch http://drupal.org/files/issues/states-multi-select-fix-1149078-3.patch can't be applied on latest 8.x-dev
Comment #7
wuinfo - bill wu commentedPatch applied if putting the patch under core folder. I will put the status to review and let bot test it first.
Comment #8
wuinfo - bill wu commented#3: states-multi-select-fix-1149078-3.patch queued for re-testing.
Comment #10
wuinfo - bill wu commentedI can reproduce the original issue in the latest 8.x with following setup:
New Drupal8.x setup with latest code with command: git pull
Built a small module with custom form and code in #3 comment
Attached patch applies to 8.x with same code as #3.
In Drupal 7.16, I can reproduce the same issue and the code in #3 is also working.
Comment #11
wuinfo - bill wu commentedchange the status
Comment #12
star-szrThanks @wuinfo! We could move this even further by cleaning up the code comments a bit as per http://drupal.org/node/1354. At a glance, I can see the comments need a space after the //, need to be a complete sentence (end in a period), and should be wrapped at 80 characters (some wrap early).
Comment #13
wuinfo - bill wu commentedCode can be a little bit better, I will work on it.
Comment #14
wuinfo - bill wu commented@Cottser comment and code are both changed. Review needed.
Comment #15
wuinfo - bill wu commentedreturn false here just exit the each loop. So there is a bug in the code.
Comment #16
wuinfo - bill wu commentedHere is a new patch which basically use #10's code and #14's comment.
Comment #17
wuinfo - bill wu commentedQuit the .each loop as early as possible by "return false;", according to http://api.jquery.com/each/
Comment #18
jelle_sMaybe add a comment that this return false is to break the loop and not to actually return false. When I first read this patch I was thinking we could replace the
return arrayComplies;with areturn true;since we returned false in the loop anyways. (But then it hit me it isn't the actual return, but it's just to break the foreach).Comment #19
wuinfo - bill wu commentedComment was added according #18
Comment #20
jlapp commentedI was able to reproduce this issue on Drupal 7.19 and manually applying the patch from #19 resolved it for me. It would be great to see this committed to 8.x and backported to 7.x.
Comment #21
nod_use jQuery $.isArray() detecting arrays is something we don't want to deal with.
don't use $.each, make a filtered for loop
don't need $.inArray(); we can use .indexOf since we don't need to care about IE8 (patch for D7 will be different, that's fine).
Pre-emptively tagging for https://drupal.org/project/ie8
Comment #22
Antoine Vigneau commentedHi,
I wake up this thread because I'm experimenting this issue on 7.21.
I want to have a #states selector matching all select element of a multi-value field. My field is an Entity Reference and the widget is a Multiple Selects List. But at the end, I normally could select all these elements using:
#field-dependee-values .form-type-select select. I expect that the dependent reacts on all select elements of the dependee, in fact it reacts to nothing (always show).The only things which works is
#field-dependee-values .form-type-select select:firstbut it works only for the first select element.Here is my #states declaration in a form_alter (it's in a node edit form):
The dependent must react to multiple values of the dependee, hence the multiple 'value' array.
I tried other kind of selector with the name too but the name attribute has this structure: name_field_dependee[und][0][target_id], so I tried
select[name="name_field_dependee[][][]"], it doen't work. I applied the patch #19 too but the comparison was never detected as an Array.Comment #23
peterpoe commentedApplied _nod's review (use for instead of $.each and indexOf instead of $.inArray).
Comment #24
hass commentedComment #25
rdeboerWould love to see this patch committed. This is quite a common use-case and has cause me a few head-aches and poor UX issues.
Comment #26
oostieWorks for me, can this patch be committed to core?
Comment #28
Tobias Xy commentedComment #29
mgiffordreroll....
Comment #30
vaza18 commentedThese patches were not working for me with multiselect selector when default value was not defined.
It became working only after adding this code (set oldValue = false instead of null) in defaultTrigger function:
Comment #31
muschpusch commentedIs it possible to use / test this functionality without patching core for D7? I extended / overwrote the 'Dependent.comparisons' function like mentioned in #4 but it doesn't seem to work.
Comment #32
mgifford@vaza18 can you roll a new patch?
Comment #33
ufku commentedFor multiple selects, the correct approach is to initiate the state value as
undefinedto make sure strict equality fails whennullis returned as a value.Need to change:
to:
I can't provide a patch now sorry.
Comment #34
mgiffordOk, I added that @ufku - we just need folks to test it now.
Comment #35
nod_The following line should be replaced with
Array.isArray(value):and we could use some fancy Array function for the other piece of code, like [].every() or something.
Comment #36
himanshupathak3 commentedComment #37
himanshupathak3 commentedAdded first requirement in code. Added patch and interdiff for it.
@nod_ the second requirement for function every is not supported by this way
[12, 5, 8, 130, 44].every(elem => elem >= 10);, is there some other way we can use that ?Comment #38
himanshupathak3 commentedComment #39
DrCord commentedI applied patch #37, it did not fix multiple selects for me :( I did apply it to drupal 7.38 though...)
Comment #40
kuntyi commentedHi all,
I tried to fix this issue. I have added solution to add logic operator, you need to add array with operator to the end of the value array, look syntax:
$form['dependent'] = array(
'#type' => 'textfield',
'#states' => array(
'visible' => array(
'select[name="dependee[]"]' => array('value' => array('a', 'c', array('xor'))),
),
),
);
You can use next operators: and (default), or and xor.
Can someone test it?
Thanks.
Comment #42
goz commentedI think we should focus on the #37 patch way to fix this issue.
@Kuntyi, your patch add a more complex and powerful solution, so may be we could talk about it in another issue. This current issue will fix the multiple select field issue, and your issue will add logic operator feature.
Comment #43
jrbIn #37, I think the
Array.isArray(value)comparison added per the suggestion in #35 should actually be!Array.isArray(value)(i.e. *not* an array). After making this change, it works for me.I've attached an updated patch.
Comment #45
jrbRerolled #43 for latest core.
Comment #46
mgiffordDoes this need the
<h3>Why this should be an RC target</h3>info & RC phase evaluation table? Also there's the "rc target triage" tag.Just trying to help this along.
Comment #47
legolasboRemoving the novice tag because I don't think there are any outstanding novice tasks remaining.
I've manually reviewed the patch. The only thing I can think of to improve this patch would be to return FALSE by default, Iterate over the values and return TRUE if they all exist. Besides that nitpick, the patch works like a charm and committing this patch would unblock #2635664: Make the hide/show of the other field work.. I therefor think that a change in logic should not hold back the resolution of this issue.
Landing this patch unblocks #2635664: Make the hide/show of the other field work., which is why I added the Contributed project blocker tag.
Comment #48
legolasboTo really take this home I've hidden some irrelevant files and made some before/after screenshots.
Comment #49
legolasboComment #50
alexpottThis really makes me wish we had frontend testing.
Also this fails our eslint validation with the following errors.
Comment #51
alexpottDidn't mean to set to 7.x
Comment #52
legolasboThanks for pointing to the eslint documentation, I updated my phpstorm config to also include eslint for future patches.
Attached patch fixes the eslint errors. Even though the changes should not change any behaviour I manually tested it again to make sure.
Back to RTBC
Comment #53
alexpottGiven we don't have frontend testing I want to see some evidence of visual regression testing because of this. This was added in #735528: FAPI #states: Fix conditionals to allow OR and XOR constructions and it was changed from being
self.values[selector][state.pristine] = undefined;so I think we might have broken the OR or XOR logic...Comment #54
legolasboAfter reading trough #735528: FAPI #states: Fix conditionals to allow OR and XOR constructions I came to the conclusion that this change in #45 was probably unneeded and not relevant for the functionality. This means that the eslint error was introduced without any reason and the offending line should not be removed, but reverted to it's original state.
I put my theory to the (manual) test and concluded that the change to the line in question can indeed be reverted without breaking the functionality added in #45. Attached you'll find a new patch which reverts the line in question back to the way it is in HEAD.
Comment #55
Anonymous (not verified) commentedThere is an issue if you pass integer values to be compared against each other.
if you map the integer values to strings then you get the correct results.
Because the default value of multiselects is null, the reevaluate passes right over it. Thinking that null is the same value as null.
I tried to come up with a solution but the best I can do is pass a default value in. That isn't a fix be any means.
EDIT: Fixed, just skip the null checks.
Comment #56
legolasboneoappt, could you roll a patch of your solution?
Comment #57
mtiftHmmm, none of the above solutions work for me. Although, I was having a problem just with checkbox example, such as the one mentioned on https://api.drupal.org/api/drupal/core%21includes%21common.inc/function/...
So maybe this was the wrong place to post. Sorry.
Comment #59
jhedstromWe now have a javascript test base, so adding the 'needs tests' tag.
Comment #60
sukanya.ramakrishnan commentedHas a fix been applied to core for this issue?
Thanks,
Sukanya
Comment #61
deranga commentedApplied patch from comment #52 to states.js on Drupal 7.50 and this works as expected now for multi-valued select field.
Will this patch ever be rolled into an update of Drupal 7?
Comment #62
deranga commentedApologies accidentally changed version number for issue.
Comment #63
legolasbo@deranga,
I believe the fix will be backported to D7 after it get's committed to 8.1.x-dev
Comment #65
sukanya.ramakrishnan commentedCan this issue be taken up again? i tried applying the patch, but it doesnt work for me. I think i am going wrong with the syntax for the states property. Can someone please put down the correct syntax for a multi select field so that i can try again. Basically i need one field to be visible based on multiple values chosen in a select field (or condition)
Thanks,
Sukanya
Comment #66
goz commentedRe-roll patch with updates from neoaptt in #55.
I also join module to help test (rename file to .tar.gz instead of .tar_.gz).
Manual tests look fine with #55 updates.
Still missing javascript test base as suggested by jhedstrom in #59.
Comment #67
goz commentedComment #68
goz commentedI'm working on tests.
There is some updates to make to #54.
Multiple property should not be used to defined null default value. Default null value is an issue for #multiple property but also #size property.
Both are specific to select list but we should find another solution more generic.
I suggest to use $this->reevaluate() at the end of states.Dependent.prototype.initializeDependee, which force analyze states conditions and apply events.
Comment #69
goz commentedFinally, i'm not sure this issue is the place to add tests. Another issue in progress is covering #states tests #2702233: [backport] Add JavaScript tests for Form API #states: required, visible, invisible, expanded, checked, unchecked so we should add #multiple tests there.
Following patch solve default null issue for both #multiple and #size attribute when i check in my browser, but mink seems to not be OK with that. Very strange, may be we should investigate in specific issue.
Comment #70
sukanya.ramakrishnan commentedWas finally able to get the patch working. Manual testing is fine for me.
Thanks
Sukanya
Comment #71
sukanya.ramakrishnan commentedComment #72
droplet commentedThe next function returns when the condition is met. We need not loop over each item.
Considered Array.some
Comment #74
leisurman commentedIve tried patch 52 and 69, but its still not working
Comment #75
Pavan B S commentedApplied the #69 patch manually and fixed the coding standard error "Line is exceeding 80 characters".
Comment #77
sukanya.ramakrishnan commented@leisurman, I found that i had to clear the browser cache in addition to drush cr to get this working.. weird, but worth giving a try!
Thanks,
Sukanya
Comment #78
leisurman commented@sukanya.ramakrishnan Which patch did you use #75. I have not tried that one.
Comment #79
leisurman commentedI cleared browser and drupal cache. Patch #75 did not work for me. I get this warning. warning php Warning: Invalid argument supplied for foreach() in conditional_fields_states_handler_select_multiple() (line 29 of /var/www/html/d818/modules/conditional_fields/conditional_fields.states.
I am using hook form alter, But I also tried the Conditional fields module, this is its issue:
https://www.drupal.org/node/2857718
This module works in Drupal 7 when the dependee field is a multiple select
Comment #80
sukanya.ramakrishnan commented@leisurman, I used the one in 69. I did not try any other module. Are u sure your syntax is correct? i had issues with my syntax and after figuring out the correct syntax, i updated the description on this issue to show the correct one (The description was missing something before, i cant recollect). Please verify your syntax against the example in the description of this issue.
Comment #81
jhedstromNeeds reroll after #2818825: Rename all JS files to *.es6.js and compile them .
Comment #82
dinesh18 commentedI tried to apply patch mentioned in #75 and it is getting applied properly with 1 white space-errors.
Do we need reroll ?
Comment #83
dinesh18 commentedhere is the gitbash screenshot
Comment #84
droplet commentedIt's better to fix #72 & #75 at the same time.
Comment #85
jofitzRe-rolled.
@droplet Can you clarify what you are requesting in #72, please?
Comment #88
sukanya.ramakrishnan commentedUpdating status to kick off tests for the 8.4 patch
Comment #89
GrandmaGlassesRopeManMultiline comment format.
Multiline comment format
Don't use `for in`. Use Object.{keys,values,entries}
Remove `hasOwnProperty` check.
Multiline comment format.
Comment #90
heddnIt isn't clear what is needed with the multi-line comments. It seems that both C style and C++ are both allowed: https://www.drupal.org/docs/develop/standards/javascript/javascript-codi...
Comment #91
droplet commentedC++ comment style is preferred to use inside the function. We missed some complete docs I thought.
https://www.drupal.org/node/1354#inline
Blocker of this issue: Missing tests
Comment #92
heddnI think this fixes the feedback from #89. And I did successful manual testing using the following. Even the red asterisks for the required showed up.
Comment #93
heddnActually, scratch that. I had temporarily removed '#multiple' from the code. It still isn't working.
Comment #95
jrockowitz commentedComment #97
igalafate commented#92 patch doesn't apply anymore after I have updated to 8.6. Has anyone run into the same problem?
Comment #98
GrandmaGlassesRopeMan- reroll for 8.7.x
Comment #99
GrandmaGlassesRopeManSince we are following AirBnB coding standards, they suggest using
/** ... */for multiline comments.Coding standards explicitly forbid using
for...inloops. Use an actual iterator instead.Additionally,
Object.entires()no Internet Explorer support. You'll need to useObject.keys{,values}instead. Somewhat related, there is ongoing discussion around adding polyfill support for missing features in JavaScript here, #2985495: JS: Polyfills supportComment #100
arnaud-brugnon commentedIf you are not patient and you still want to add multiple values,
You should check this link https://evolvingweb.ca/blog/extending-form-api-states-regular-expressions
It's for Drupal 7 but it works for D8 with some adjustment.
Comment #101
dalinHere's a re-roll of the patch that applies to 8.6.
This does not address the issues brought up in #99.
Comment #102
dalinOops, re-roll the patch for 8.6 with the correct git root.
Comment #103
daniel kulbeThis does currently not work due Error:
ReferenceError: _slicedToArray is not definedComment #104
daniel kulbeUpdated patch
Comment #105
ckidowIt seems that the Patch #80 from following issue solves this issue as well: https://www.drupal.org/node/1091852
Comment #106
attiks commented#105 Patch from #1091852-80: Display Bug when using #states (Forms API) with Ajax Request didn't work for me, but patch from #104 works with the following code
Comment #107
panchoPatch #104 seems to fail 1 out of 3 tests of the Nightwatch JS statesTest, but otherwise passes all tests.
Comment #109
uzlov commentedFixed issue, tested with another types of depended elements.
Like:
Removed:
Please, check
Comment #110
esod commentedBoth #109 and #102 apply cleanly to our Drupal 8.6.16 system. Neither patch fixes the issue, but we're using Chosen, which may be interfering.
Comment #111
andrewmriv commentedCan also confirm that #104 did not work for me on Drupal 8.7. Also have Chosen enabled, though so I may also just be affected by that.
Comment #112
esod commentedUsing this form code with or without any patches applied :
I get this error:
Comment #113
esod commentedNever mind the last comment. It should be
'select[name="field_my_multi_select_field[]"]', not':select[name="field_my_multi_select_field[]"]':selectmust be a pseudo selector.Patch #109 still doesn't work. :(
Comment #114
pfenriquez commented@esod
Patch #109 worked for me (using also Chosen), but you must pass the value as an array of 'value'
Please note the ['my_value']
Best
edit: removed pseudo selector for example.
Comment #115
pfenriquez commentedComment #116
esod commented@Penef. Thanks. Yes the patch does indeed work when you pass the value as an array of 'value'. Hooray!
The tip off was the three close brackets after
my_valueon the select line.Comment #117
esod commentedThe patch still applies cleanly to Drupal 8.7.7, but the patch has stopped working when using a
<select>tag.Comment #118
esod commentedActually, never mind my last comment. We had another, unrelated js issue on our site. Patch works fine for us in Drupal 8.7.7!
Comment #121
jungleMight be a random failure
Comment #123
phjouIt seems to work for a specific value, but how do you make it work to check that it is not empty?
I tried multiple stuff and nothing seems to work.
This one would seem logical for me, but it does not work.
Comment #124
dwwThis issue 'Needs tests' before it could be committed.
We have an RTBC core issue that adds a bunch of great
#statestests: #2702233: [backport] Add JavaScript tests for Form API #states: required, visible, invisible, expanded, checked, uncheckedThat would be the perfect place to add the tests that this issue sorely needs.
Therefore, this should be postponed until that one lands.
Also, giving this issue a proper summary with accurate 'Remaining tasks' section.
Thanks!
-Derek
Comment #126
kevin.dutra commentedThe issue this was postponed on appears to have been committed, so returning to "needs work" for the addition of tests.
Comment #128
leon kessler commentedI'm having the same issue as #123, I can't get this to work when targeting an empty multiple value select field.
I've tried
['value' => []]['value' => '']['empty' => TRUE]['filled' => FALSE]None of these work.
Comment #129
marios anagnostopoulos commentedThe way #109 is implemented, if the reference is empty (E.g
['value' => []]), the condition returned is always true, regardless if the given value (actual field selections) is empty or not, because it never goes through the loop.I uploaded a different patch using the Array.prototype.every for executing the loop, which seems to handle the issue.
It does not work with
['value' => ''] | ['empty' => TRUE] | ['filled' => FALSE], like the #109 does not.For the
['value' => '']it shouldn't anyway since it would be semantically wrong. Now if we should support the empty and filled options, is debatable, but I think that['value' => []]should cover it.Let me know if the patch solves the issue in your projects as well, or if it needs more tuning.
Comment #130
marios anagnostopoulos commentedHad to change the paths for the patch to apply, my bad.
Comment #131
marios anagnostopoulos commentedFixed an issue with prettier
Comment #132
danflanagan8Here's a test-only patch to expose the bug and then work against. It has a couple especially notable cases.
1. There's one for the empty value, which is a case that there are several comments about.
The patch in 109 cannot handle this. The patch in 130 handles this well.
2. There's a case for an OR condition.
This OR case especially is interesting. I'm not sure if that's the right syntax or if there even is an agreed upon syntax. The patch in 109 makes this work like an OR. The patch in 130 makes this work like an XOR.
The test performs an assertion as if it should work like OR. It's possible that's an incorrect expectation on my part. I can't say I'm an expert on
#states.Comment #134
marios anagnostopoulos commentedIndeed this is an issue. This syntax should not work like XOR (I will look into it when I find the time).
The thing is that multi-value fields are new to the states API I think and should have a bit more care in their handling.
We should introduce a way to define if we want the value, given in the reference array, to be strictly compared or not.
This problem extends also to cases like this.
Let's say we have a multi-select field and set some state to check.
['value' => ['val1', 'val2']]The way the code works atm dictates that the condition is fulfilled if AT LEAST BOTH of these two values are present. Meaning that
['val1', 'val2', 'val3']input will also fulfill the condition. It is not clear that this is an intended behavior (Not to me at least).We should address this issue first IMO and then handle the conjunction between multiple conditions.
Comment #135
danflanagan8It is not clear to me either. :)
Does that mean we need to get a subsystem maintainer review? I'm going to add that tag. We can remove it if I'm wrong!
I think as this issue moves along, we'll need to add a few test cases and/or modify the expectations of the existing ones, but I'm going to remove the Needs tests tag since the tests exist, even if they may require some tweaks.
Comment #137
stijndmd commentedI'm trying this patch in combination with inline entity forms.
Comment #139
hmendes commentedI tested the patch from #131 using the custom fields of the IS and doing a few changes to test.
Here are my results:
1:
'select[name="dependee[]"]' => ['value' => ['a']],It will only show the field if I select EXACTLY A.
Selecting 2 (doesn't matter which options are selected) or 3 options -> is not showing the field
2:
'select[name="dependee[]"]' => ['value' => ['a', 'b']],It will only show the field if I select EXACTLY A and B
Selecting 2 (doesn't matter which options are selected) or 3 options -> is not showing the field
3:
'select[name="dependee[]"]' => ['value' => ['c', 'a', 'b']],It will only show the field if I select EXACTLY A, B and C
4:
'select[name="dependee[]"]'=> [['value' => ['a']], ['value' => ['b']]],It will only show the field if I select EXACTLY A or EXACTLY B (if I select A and B it won't show the field)
5:
'select[name="dependee[]"]'=> [['value' => ['a']], ['value' => ['b']], ['value' => ['c']]],It will only show the field if I select EXACTLY A, EXACTLY B or EXACTLY C (if I select two or three options at the same time it won't show the field)
Tested all tests above but now setting a default value for the field -> did not interfere with the result
So I didn't understand about the XOR behavior... Did i do some wrong testing?
Comment #140
stijndmd commentedI can confirm those results from https://www.drupal.org/project/drupal/issues/1149078#comment-14590562, as explained in https://www.drupal.org/project/drupal/issues/1149078#comment-14279315
Seems like this can not be expected behaviour since you want subfields to appear from the moment their selector has been tagged in the multi-select field used for the conditions. Still works with patch from #109 which still applies to core 9.3.
Comment #142
cobadger commentedAttaching patch against Drupal 9.5.
Comment #143
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #144
_utsavsharma commentedTried to fix CCF for 9.5.x in #142.
Comment #145
abelass#144 doesn't work on 9.4.11
#130 does work for me
Comment #146
abelpzl#142 does work for me in 9.5.3 with php 7.4
Comment #147
gauravvvv commentedI have attached patch for 10.1.x. none of the above patch is applying to 10.1.x. So not attaching interdiff. please review
Comment #148
ysamoylenko commentedConfirming that https://www.drupal.org/project/drupal/issues/1149078#comment-14271390 works correctly with empty values on 9.5.3.
Comment #149
nod_I don't have a good idea about what should be the expected behavior, and I don't have the time to get into it this weekend. One the other hand, one small review:
key is never used,
Object.values()would be more appropriate than entries.This could be simplified by using
includes()Also not sure what that loop is doing, returing true or false inside the foreach does not do anything. I see #142 was using a for loop, in that case returning true/false would make sense. Not with forEach().
I would definitely expect some tests cases here just to make sure we agree on the behavior.
Comment #150
akram khanadded updated patch address #149
Comment #151
akram khantry to fixed CCF #150
Comment #152
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #153
gauravvvv commentedAddressed #149, attached interdiff for same. please review
Comment #154
gauravvvv commentedFixed build issue.
Comment #155
gauravvvv commentedWrong patch uploaded in #153. Updated the patch in #154.
Comment #156
saurabh-2k17 commented#144 did not work in my case. I later used #142 which fixed the problem. My Drupal Core version is 9.5.7
Comment #159
vladimirausSwitching to MR.
Comment #160
neslee canil pinto@VladimirAus Object.values will give referenceValue undefined, inside if we use Object.entries it will work fine with key, value pair
Comment #161
arnaud-brugnon commentedImprove patch based on #160 comments
By the way, it works fine for me.
Comment #162
smustgrave commentedSeems tests have been lost.
Comment #165
vasikeUpdated the MR - trying to add the tests from #132
So:
- Add tests
- Change compare - The arrays values should match - it should work also for empty arrays.
- Fix a test ... i think the OR check there was not right if value2 OR value3 doesn't mean if should be displayed when both value2 and value3 selected.
For OR case we should something if (value2 and value3) OR (value7 and value32)
Note: i only used tests to check the "solution" ... i didn't test manually.
Comment #166
dwwThanks, great progress!
About out the door for a few AFK meetings, but I'll try to look more closely when I'm back. Super quick skim finds some nits with the tests (wrong comments, etc). Also, the other #states tests in these files are now starting to do:
The test in the MR is currently stopping at step 3 (like many, but not all of the other protected helpers)...
Comment #167
dwwUgh, that's super annoying. 😢 I worked on this locally. To my great surprise, 2 of the "initial state" assertions are failing after I remove the values in the multi-select trigger:
$this->assertTrue($item_visible_no_value->isVisible());$this->assertFalse($textfield_visible_value2_and_value3->isVisible());If I comment those out, the test passes. If either are still present, the test fails. However, when I inspect the HTML output of the failing page, I see it looks like it's working as expected:
The multi-select indeed has no values, and the only thing visible in the section of tested elements is what
$item_visible_no_valuepoints to. The rest of the test is working fine. Manually testing the multi-select trigger from this HTML result page and all the JS seems to be working as expected, too.I'm not sure this is actually a bug in the states code -- it could just be a bug in the test. But I'm out of time for today to keep digging.
Sorry,
-Derek
Comment #168
dwwGrr, phpcs didn't like how I commented out the failing assertions. 😢
I fixed that and pushed a commit with the failing assertions included. That run should fail.
Pushed another where I properly commented out.
core/scripts/dev/commit-code-check.shis now passing locally. 😅Also rebased to the latest 10.1.x branch. We probably need to change the MR to point to 11.x for now. However, I just verified that the MR plain diff applies cleanly to the 11.x branch, too (as expected), so I'm not going to mess with trying to get the MR target branch changed (which I don't have permissions to do).
Comment #169
vasikei confirm that the js is working properly ... but it seems the
$trigger->setValue([]);doesn't work properly.So what should we do ... find another way to "deselect" the multiple selection?
What about the chasing this "odd case" in a separate issue.
And try to commit the "current solution"?
Comment #170
dwwYeah, I hate holding up a fix in the quest for "perfect" tests. Definitely open to moving the last bits to a follow-up...
However, in more closely inspecting the HTML output of the tested page, the initial state's markup for the
itemlooks like this:After the test fails, the final output page has this for the element:
The only difference is that the one that's failing the test assertion includes this:
style="". That shouldn't matter, but maybe it does?I'll also note that while inspecting the HTML on the failed test output, I'm getting 5 of these:
All 5 of the affected nodes are the
itemelements. That seems completely unrelated to this issue or bug, but wanted to note it here. Probably we need a follow-up to investigate this part, too.Comment #171
dwwThanks for the review and fixes. Almost there! I opened #3367310: Get all assertions working in JavascriptStatesTest::doMultipleSelectTriggerTests() as the follow-up to sort out the final test weirdness, and pushed a commit to add the @see comment pointing to it as part of the @todo.
I believe all feedback is now addressed, so this is ready for (hopefully final?) review. Removing the "Needs tests" tag, since that's now done.
Thanks again!
-Derek
p.s. Before commit, this issue definitely needs a review of issue credits. There are a bunch of 1-off patches in here that probably shouldn't get credits, and a couple of folks who commented a lot but haven't uploaded anything who might need to be credited. If I have a chance later today, I'll work on this, but if I don't get to it, someone needs to before this is truly ready.
Comment #172
damienmckennaThe latest merge request in patch format for 9.5.x.
Comment #173
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #174
nod_Comment #175
sukanya.ramakrishnan commentedSlightly confused here, I am looking for a solution for the scenario 4 in this comment
https://www.drupal.org/project/drupal/issues/1149078#comment-14590562
'select[name="dependee[]"]'=> [['value' => ['a']], ['value' => ['b']]],
It will only show the field if I select EXACTLY A or EXACTLY B (if I select A and B it won't show the field)
I need the field to be displayed if I select A and B (in addition to other values not in the conditions) . I applied the latest patch and still see the issue. Has this issue been addressed in the latest patch/ am i missing something or is it going to be addressed separately?
Thanks,
Sukanya
Comment #176
vasike@sukanya.ramakrishnan maybe at first sight
For
'select[name="dependee[]"]'=> [['value' => ['a']], ['value' => ['b']]],We expect to work also for both
aandbselectedBut it's not true ... because it's about the value(s) of the element.
And actually we have:
[a,b] !== [a]and[a,b] !== [b]Which means that maybe you need something different like
'select[name="dependee[]"]'=> [['value' => ['a']], ['value' => ['b'], ['value' => ['a', 'b']]],To cover all the Multiple element value(s) scenarios.
P.S. remember, we're talking about MULTIPLE and we're using SET of Values ... not Parts of Values
i hope i got it right :).
Comment #177
smustgrave commentedStill needs submaintainer review but @dww could the MR be updated for 11.x please?
Comment #178
mukhtarm commentedAny idea on how to fix this issue, I applied the MR (https://git.drupalcode.org/project/drupal/-/merge_requests/3805/diffs) and still doesn't resolve the issue.
for eg:
Why i given the name so?: when inspected the element the name for the element displayed so. Also when giving
name="payment_methods][]"also not working.Comment #179
abelassIs there any solution for 10.1.5 ?
Comment #180
vasike@MukhtarM i think you're trying "wrong selector"
':input[name=..."when here it's about
select'select[name=..."could you, please, check?
thanks
Comment #182
vasikenew MR for 11.x available ... all updated and "Green", so i think we're back to Needs Review
Comment #183
mukhtarm commentedThanks @vasike, The issue is fixed for me after applied the latest MR(https://git.drupalcode.org/project/drupal/-/compare/308e696f76fc33d6a5d8...).
Exactly was in the section:
Correct solution for me is:
if that helps anybody in future. This will show and hide the fieldset when either
'cvs'or'payeasy'is selectedComment #184
ronaldtebrake commentedCan confirm for us https://git.drupalcode.org/project/drupal/-/merge_requests/3805 also did the trick (for D10.x)
I've attached a static patch of the current state of the MR for CI/CD purposes.
Comment #185
ericbellot commentedFor me, on Drupal 10.1.6, the patch #184 does not work completely. I had to go back to version #183 of states.js.
Sorry, my patch is useless. The patch #161 works correctly on Drupal 10.1.
Comment #186
smustgrave commentedAssigning to javascript submaintainer.
Comment #187
nod_Works for me, it's simple enough and does the job advertised. Thanks for the tests too!
Comment #188
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #189
vasikeit seems we need to hide the patch from #185
back to previous Status ... RTBC
Comment #190
nod_There is a todo left in the tests code
Comment #191
nod_Comment #196
nod_Thanks everyone for keeping this going for so long, it's finally in :)
Committed f6be853 and pushed to 11.x. Thanks! Backported to 10.2.x and 10.3.x
Comment #197
dwwwhoot, thanks!
Comment #199
n.dhuygelaere commentedSorry, but the last version of Drupal 10.3.1 break all my "select" based conditional fields.
My conditional fields works well on Drupal 10.2.6
Here an extract from my form where the field_child is visible when the taxonomy term (tid:1234) is selected
Comment #200
n.dhuygelaere commentedI answer to myself. finally i've solved my issue by fixing 2 points:
The first point was a mistake from my side : the "value" used in the definition must be a "string". In my code, i used an entity reference id defined as an integer.
The second point was linked to my select field defined as a "multiple" one. To address this point it's necessery to alter the "compare" function of core/misc/states.js. I add this js library into my module
This solution works with all drupal version from 8.x to 10.3.1.