Skip to content

Commit

Permalink
work around some validation exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Groszer committed Sep 12, 2012
1 parent f4cc69e commit ea4676d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGES
with an EditForm after save as it was before editing.
That brings some changes with it:
- *MAJOR*: unchanged values/fields do not get validated anymore
(unless they are empty or are FileUploads)
- A temporary ``SimpleTerm`` gets created for the missing value
Title is by default "Missing: ${value}". See MissingTermsMixin.

Expand Down
22 changes: 21 additions & 1 deletion src/z3c/form/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class SimpleFieldValidator(StrictSimpleFieldValidator):

def validate(self, value, force=False):
"""See interfaces.IValidator"""
if value is self.field.missing_value:
# let missing values run into stricter validation
# most important case is not let required fields pass
return super(SimpleFieldValidator, self).validate(value, force)

if not force:
if value is interfaces.NOT_CHANGED:
# no need to validate unchanged values
Expand All @@ -100,7 +105,21 @@ def validate(self, value, force=False):
return

# otherwise StrictSimpleFieldValidator will do the job
return super(SimpleFieldValidator, self).validate(value)
return super(SimpleFieldValidator, self).validate(value, force)


class FileUploadValidator(StrictSimpleFieldValidator):
"""File upload validator
"""
zope.component.adapts(
zope.interface.Interface,
zope.interface.Interface,
zope.interface.Interface,
zope.schema.interfaces.IBytes,
interfaces.IFileWidget)
# only FileUploadDataConverter seems to use NOT_CHANGED, but that needs
# to be validated, because file upload is a special case
# the most special case if when an ad-hoc IBytes field is required


def WidgetValidatorDiscriminators(
Expand All @@ -126,6 +145,7 @@ class NoInputData(zope.interface.Invalid):
"""


class Data(object):
zope.interface.implements(interfaces.IData)

Expand Down
14 changes: 12 additions & 2 deletions src/z3c/form/validator.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ well-defined. Thus, we first need to create a schema.
... login = zope.schema.TextLine(
... title=u'Login',
... min_length=1,
... max_length=10)
... max_length=10,
... required=True)
...
... email = zope.schema.TextLine(
... title=u'E-mail')
Expand Down Expand Up @@ -186,7 +187,7 @@ again:
... interfaces.IValidator)

Ignoring unchanged values
~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~

Most of the time we want to ignore unchanged fields/values at validation.
A common usecase for this is if a value went away from a vocabulary and we want
Expand Down Expand Up @@ -242,6 +243,15 @@ Unless we want to force validation:
...
TooLong: (u'hippocratiusxy', 10)

Some exceptions:

``missing_value`` gets validated

>>> simple.validate(IPerson['login'].missing_value)
Traceback (most recent call last):
...
RequiredMissing: login


Widget Validators and File-Uploads
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions src/z3c/form/validator.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<adapter
factory=".validator.SimpleFieldValidator"
/>
<adapter
factory=".validator.FileUploadValidator"
/>
<adapter
factory=".validator.InvariantsValidator"
/>
Expand Down

0 comments on commit ea4676d

Please sign in to comment.