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

Commit

Permalink
Fixes an error where optional fields had to be filled out
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
Denis Krienbühl committed Sep 10, 2015
1 parent 9c2d127 commit 7b20065
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
Unreleased
~~~~~~~~~~

- Fixes an error where optional fields had to be filled out.
[href]

- Adds rudimentary syntax checking with information about which line wrong.
[href]

Expand Down
4 changes: 3 additions & 1 deletion onegov/form/parser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@
TextAreaField
)
from wtforms.fields.html5 import DateField, DateTimeLocalField, EmailField
from wtforms.validators import DataRequired, Length
from wtforms.validators import DataRequired, Length, Optional
from wtforms.widgets import TextArea
from wtforms_components import Email, If, TimeField

Expand Down Expand Up @@ -733,6 +733,8 @@ def add_field(self, field_class, label, required,
validator.field_flags = ('required', )

validators.insert(0, validator)
else:
validators.insert(0, Optional())

# try to find a smart field_id that contains the dependency or the
# current fieldset name - if all fails, an error will be thrown,
Expand Down
30 changes: 28 additions & 2 deletions onegov/form/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from textwrap import dedent
from webob.multidict import MultiDict
from wtforms import FileField
from wtforms import validators
from wtforms.fields.html5 import (
DateField,
DateTimeLocalField,
Expand All @@ -30,12 +31,16 @@ def test_parse_text():

assert form.first_name.label.text == 'First name'
assert len(form.first_name.validators) == 1
assert isinstance(form.first_name.validators[0], validators.DataRequired)

assert form.last_name.label.text == 'Last name'
assert len(form.last_name.validators) == 0
assert len(form.last_name.validators) == 1
assert isinstance(form.country.validators[0], validators.Optional)

assert form.country.label.text == 'Country'
assert len(form.country.validators) == 1
assert len(form.country.validators) == 2
assert isinstance(form.country.validators[0], validators.Optional)
assert isinstance(form.country.validators[1], validators.Length)

assert form.comment.label.text == 'Comment'
assert form.comment.widget(form.comment) == (
Expand Down Expand Up @@ -345,6 +350,27 @@ def test_stdnum_field():
assert form.errors


def test_optional_date():

text = "Date = YYYY.MM.DD"

form_class = parse_form(text)

form = form_class(MultiDict([
('date', '30.02.2015')
]))

form.validate()
assert form.errors == {'date': ['Not a valid date value']}

form = form_class(MultiDict([
('date', '')
]))

form.validate()
assert not form.errors


def test_invalid_syntax():
with pytest.raises(errors.InvalidFormSyntax) as e:
parse_form(".")
Expand Down

0 comments on commit 7b20065

Please sign in to comment.