Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Adds the ability to depend on not a specific field value
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Sommerhalder committed Jun 8, 2016
1 parent de42458 commit 0e0c1b9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
---------

- Adds the ability to depend on not a specific field value.
[msom]

0.11.2 (2016-05-11)
~~~~~~~~~~~~~~~~~~~

Expand Down
8 changes: 5 additions & 3 deletions onegov/form/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,19 @@ class FieldDependency(object):

def __init__(self, field_id, choice):
self.field_id = field_id
self.choice = choice
self.raw_choice = choice
self.invert = choice.startswith('!')
self.choice = choice[1:] if self.invert else choice

def fulfilled(self, form, field):
return getattr(form, self.field_id).data == self.choice
return (getattr(form, self.field_id).data == self.choice) ^ self.invert

def unfulfilled(self, form, field):
return not self.fulfilled(form, field)

@property
def html_data(self):
return {'data-depends-on': '/'.join((self.field_id, self.choice))}
return {'data-depends-on': '/'.join((self.field_id, self.raw_choice))}


def merge_forms(*forms):
Expand Down
18 changes: 18 additions & 0 deletions onegov/form/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class TestForm(Form):
depends_on=('switch', 'on')
)

inverted = TimeField(
label="Inverted",
validators=[],
depends_on=('switch', '!on')
)

request = DummyRequest({'switch': 'off'})
form = TestForm(request.POST)
assert form.validate()
Expand Down Expand Up @@ -133,6 +139,18 @@ class TestForm(Form):
form = TestForm(request.POST)
assert form.validate()

request = DummyRequest({'switch': 'on', 'inverted': '12:00'})
form = TestForm(request.POST)
assert not form.validate()

request = DummyRequest({'switch': 'off', 'inverted': '12:00'})
form = TestForm(request.POST)
assert form.validate()

request = DummyRequest({'switch': 'off', 'inverted': 'asdf'})
form = TestForm(request.POST)
assert not form.validate()


def test_merge_forms():

Expand Down

0 comments on commit 0e0c1b9

Please sign in to comment.