Skip to content

Commit b8cfe2a

Browse files
committed
Fix the support for conditions when their trigger field is a multiple choice field
1 parent fa53ca5 commit b8cfe2a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ master (unreleased)
66
===================
77

88
- DOC: removed ``setup.py`` Python2-related classifiers.
9+
- Fix the support for conditions when their trigger field is a multiple choice field.
910

1011
Release 4.0.0 (2020-01-08)
1112
==========================

formidable/forms/conditions.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ def convert_values(field_id, values):
4040
field = fields[field_id]
4141
except KeyError:
4242
return []
43+
44+
# Define that we have a multiple field or not.
45+
multiple = hasattr(field, "choices") and \
46+
field.widget.allow_multiple_selected
47+
if multiple:
48+
# Multiple fields accept lists only as values.
49+
return field.to_python(values)
4350
else:
4451
return [field.to_python(value) for value in values]
4552

@@ -76,6 +83,17 @@ def __new__(mcls, name, base, attrs):
7683
return condition_class
7784

7885

86+
def operator_eq(ref_value, values):
87+
"""
88+
Compare if the field values is in the condition values.
89+
"""
90+
if isinstance(ref_value, list):
91+
for v in values:
92+
if v in ref_value:
93+
return True
94+
return ref_value == values[0]
95+
96+
7997
class ConditionTest(object):
8098
"""
8199
Test that is evaluated to know if the action of a :class:`Condition` can be
@@ -84,10 +102,7 @@ class ConditionTest(object):
84102
defined in `mapper`
85103
"""
86104

87-
#: dict of supported operators and their comparison function
88-
mapper = {
89-
'eq': lambda field, values: field == values[0],
90-
}
105+
mapper = {'eq': operator_eq}
91106

92107
def __init__(self, field_id, operator, values):
93108
self.field_id = field_id

0 commit comments

Comments
 (0)