Problem/Motivation

When we set the Value input mode with any of the Set of values (All there values(AND), Any of these values (OR), Only one of these values (XOR) or None of these values (NOT)) with the value 0, it is not working, and is being treated as null.

Steps to reproduce

Create a conditional field for any field
Values input mode: anything under "Set of values"
Set of values: 0

It will work if the field controlled by is a number field, but is not going to work if it is controlled by any field as a select list or radio widget.

Proposed resolution

Change where the validation of the field is with the method empty (this php method treats 0 as being empty) to test the variable with the "0" value.

Remaining tasks

Review the patch

Comments

hmendes created an issue. See original summary.

hmendes’s picture

Assigned: hmendes » Unassigned
Issue summary: View changes
StatusFileSize
new1.66 KB

Addding the patch, please review.

chucksimply’s picture

#2 didn't fix the issue on my setup.

hmendes’s picture

Adding a new patch.
There's still having with the Value input mode with any of the Set of values, but this issue I'm going to solve specifically the problem addressing the "0" value not being saved.

The problem that's still happening is addressed by #3109227: Conditional Fields not showing for multiple value select fields on node edit form with option 'Any of these values (OR)...' (but not only with the OR condition) and #1149078: States API doesn't work with multiple select fields

jessicacs’s picture

StatusFileSize
new1.71 KB

Re-roll of the patch #4.

takuma shimabukuro’s picture

Assigned: Unassigned » takuma shimabukuro
takuma shimabukuro’s picture

Assigned: takuma shimabukuro » Unassigned
Status: Needs review » Reviewed & tested by the community

I reviewed the patch, so it's enough to validate 0.
moving status for RTBC

colan’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/src/ConditionalFieldsFormHelper.php
@@ -154,7 +154,7 @@ class ConditionalFieldsFormHelper {
+      if ((!empty($options['values']) || $options['values'] == "0") && is_string($options['values'])) {

Because we're duplicating this code in a few places, which is a terrible idea, let's move it to a method like notEmpty(), and then call it from each location.

andregp’s picture

Assigned: Unassigned » andregp

@colan I'll take a look at this

andregp’s picture

Assigned: andregp » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.97 KB
new1.83 KB
new2.92 KB
new2.65 KB

I ran a search with the regex !empty\([\s\S]*\) \|\| [\s\S]* == "0" to check how many times the code !empty($options['values']) || $options['values'] == "0" or similar appeared on the module files. It appears three times, once in src/ConditionalFieldsFormHelper.php and twice in src/Form/ConditionalFieldEditForm.php. Because it only appears more than once inside ConditionalFieldEditForm.php I created the new function there and left ConditionalFieldsFormHelper.php untouched.

But if the intention was to replace all three instances with the notEmpty function then a Trait may be a better approach. (So I created a second patch using a trait, just in case).

elber’s picture

Assigned: Unassigned » elber
elber’s picture

Hi I revised and applied the patch #17 and when we set the value 0 on the "Set of values" is working now and then the issue was resolved.

I'm going to change the issue's status for RTBC.

But I think it's better the maintainer to chose if he will to commit the patch with or without traits.

elber’s picture

Assigned: elber » Unassigned
Status: Needs review » Reviewed & tested by the community
dqd’s picture

Status: Reviewed & tested by the community » Needs review

This needs another review and further discussion since some years are gone and the previously discussed things have been partly adressed and new questions have raised around them. So new thoughts are welcome.

benstallings’s picture

Status: Needs review » Needs work