Skip to content

Commit

Permalink
A counter is no longer required for multiple forms
Browse files Browse the repository at this point in the history
  • Loading branch information
ezag committed Oct 22, 2021
1 parent 006a65a commit 5100c02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
22 changes: 7 additions & 15 deletions deformdemo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2550,35 +2550,27 @@ class Schema(colander.Schema):
@view_config(renderer="templates/form.pt", name="multiple_forms")
@demonstrate("Multiple Forms on the Same Page")
def multiple_forms(self):
import itertools

# We need to make sure the form field identifiers for the two
# forms do not overlap so accessibility features continue to work,
# such as focusing the field related to a legend when the
# legend is clicked on.
# We do so by creating an ``itertools.count`` object and
# passing that object as the ``counter`` keyword argument to
# the constructor of both forms. As a result, the second
# form's element identifiers will not overlap the first
# form's.

counter = itertools.count()
# We do so by passing distinct ``formid`` keyword argument to the
# constructor of both forms. As a result, form's element identifiers
# will inherit parent's ``formid`` and will not overlap.
# (Note: alternatively, we could create an ``itertools.count`` object
# and pass that object as the ``counter`` keyword argument.)

class Schema1(colander.Schema):
name1 = colander.SchemaNode(colander.String())

schema1 = Schema1()
form1 = deform.Form(
schema1, buttons=("submit",), formid="form1", counter=counter
)
form1 = deform.Form(schema1, buttons=("submit",), formid="form1")

class Schema2(colander.Schema):
name2 = colander.SchemaNode(colander.String())

schema2 = Schema2()
form2 = deform.Form(
schema2, buttons=("submit",), formid="form2", counter=counter
)
form2 = deform.Form(schema2, buttons=("submit",), formid="form2")

html = []
captured = None
Expand Down
12 changes: 6 additions & 6 deletions deformdemo/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3498,22 +3498,22 @@ class MultipleFormsTests(Base, unittest.TestCase):

def test_render_default(self):
self.assertEqual(
findid_view("deformField1").get_attribute("name"), "name1"
findid_view("form1Field1").get_attribute("name"), "name1"
)
self.assertEqual(
findid_view("deformField1").get_attribute("value"), ""
findid_view("form1Field1").get_attribute("value"), ""
)
self.assertEqual(findid("deformField3").get_attribute("name"), "name2")
self.assertEqual(findid("deformField3").get_attribute("value"), "")
self.assertEqual(findid("form2Field1").get_attribute("name"), "name2")
self.assertEqual(findid("form2Field1").get_attribute("value"), "")
self.assertRaises(NoSuchElementException, findcss, ".is-invalid")

def test_submit_first(self):
findid("deformField1").send_keys("hey")
findid("form1Field1").send_keys("hey")
findid("form1submit").click()
self.assertEqual(eval(findid("captured").text), {"name1": "hey"})

def test_submit_second(self):
findid("deformField3").send_keys("hey")
findid("form2Field2").send_keys("hey")
findid("form2submit").click()
self.assertEqual(eval(findid("captured").text), {"name2": "hey"})

Expand Down

0 comments on commit 5100c02

Please sign in to comment.