summaryrefslogtreecommitdiff
path: root/webtest/forms.py
diff options
context:
space:
mode:
authorCarl Meyer <carl@oddbird.net>2013-08-30 20:05:48 -0600
committerCarl Meyer <carl@oddbird.net>2013-08-30 20:05:48 -0600
commitd009e5baabc502abe79730b73279e349a4539135 (patch)
tree88aed531d95925f936b65463c7e2695ecd3f9e74 /webtest/forms.py
parentdcfae9d18833a04f9b7d742fb7d7c6f26eee2923 (diff)
downloadwebtest-d009e5baabc502abe79730b73279e349a4539135.tar.gz
Allow assigning a list to a set of same-named checkboxes.
Diffstat (limited to 'webtest/forms.py')
-rw-r--r--webtest/forms.py18
1 files changed, 14 insertions, 4 deletions
diff --git a/webtest/forms.py b/webtest/forms.py
index 98c306f..8875dd3 100644
--- a/webtest/forms.py
+++ b/webtest/forms.py
@@ -427,6 +427,10 @@ class Form(object):
"""Set the value of the named field. If there is 0 or multiple fields
by that name, it is an error.
+ Multiple checkboxes of the same name are special-cased; a list may be
+ assigned to them to check the checkboxes whose value is present in the
+ list (and uncheck all others).
+
Setting the value of a ``<select>`` selects the given option (and
confirms it is an option). Setting radio fields does the same.
Checkboxes get boolean values. You cannot set hidden fields or buttons.
@@ -437,10 +441,16 @@ class Form(object):
assert fields is not None, (
"No field by the name %r found (fields: %s)"
% (name, ', '.join(map(repr, self.fields.keys()))))
- assert len(fields) == 1, (
- "Multiple fields match %r: %s"
- % (name, ', '.join(map(repr, fields))))
- fields[0].value = value
+ all_checkboxes = all(isinstance(f, Checkbox) for f in fields)
+ if all_checkboxes and isinstance(value, list):
+ values = set(utils.stringify(v) for v in value)
+ for f in fields:
+ f.checked = f._value in values
+ else:
+ assert len(fields) == 1, (
+ "Multiple fields match %r: %s"
+ % (name, ', '.join(map(repr, fields))))
+ fields[0].value = value
def __getitem__(self, name):
"""Get the named field object (ambiguity is an error)."""