Hello Michael,
After some simple thinking there are some fears that this wrecks other
basic fields validation.
Something like the field is required, has a default value but is
omitted on input. What will be the result? Accepting the default value
instead of raising an error is definitely a problem.
Also, values in the system might not be needed to re-validate?
Image passing around a 100mb uploaded file.
If this is meant only for a file upload field, then I think there should
be a special validator registered for those.
Would be nice to see some (functional) tests closing out the above fears.
Functional, because on the unittest level you can force the widget to
a lot of unexpected things, but that might not be inline with what
happens when it's really done by the framework.
Or better said, revert the changes locally, add those tests, apply the
changes back and see whether the tests still pass.
Sunday, May 17, 2009, 3:21:19 PM, you wrote:
MH Log message for revision 100028:
MH merged branch icemac_validate_NOT_CHANGED
MH
MH Changed:
MH U z3c.form/trunk/AUTHOR.txt
MH U z3c.form/trunk/CHANGES.txt
MH U z3c.form/trunk/src/z3c/form/validator.py
MH U z3c.form/trunk/src/z3c/form/validator.txt
MH -=-
MH Modified: z3c.form/trunk/AUTHOR.txt
MH ===
MH --- z3c.form/trunk/AUTHOR.txt 2009-05-17 13:18:11 UTC (rev 100027)
MH +++ z3c.form/trunk/AUTHOR.txt 2009-05-17 13:21:18 UTC (rev 100028)
MH @@ -11,10 +11,11 @@
MH Daniel Nouri
MH Darryl Cousins
MH Herman Himmelbauer
MH +Jacob Holm
MH Laurent Mignon
MH Malthe Borch
MH Marius Gedminas
MH Martijn Faassen
MH Michael Howitz
MH Michael Kerrin
MH -Paul Carduner
MH +Paul Carduner
MH \ No newline at end of file
MH Modified: z3c.form/trunk/CHANGES.txt
MH ===
MH --- z3c.form/trunk/CHANGES.txt 2009-05-17 13:18:11 UTC (rev 100027)
MH +++ z3c.form/trunk/CHANGES.txt 2009-05-17 13:21:18 UTC (rev 100028)
MH @@ -150,7 +150,11 @@
MH
MH - Bug: Don't cause warnings in Python 2.6.
MH
MH +- Bug: `validator.SimpleFieldValidator` is now able to handle
MH + `interfaces.NOT_CHANGED`. This value is set for file uploads when
MH + the user does not choose a file for upload.
MH
MH +
MH Version 1.9.0 (2008-08-26)
MH --
MH
MH Modified: z3c.form/trunk/src/z3c/form/validator.py
MH ===
MH --- z3c.form/trunk/src/z3c/form/validator.py2009-05-17 13:18:11 UTC
(rev 100027)
MH +++ z3c.form/trunk/src/z3c/form/validator.py2009-05-17 13:21:18 UTC
(rev 100028)
MH @@ -42,9 +42,28 @@
MH
MH def validate(self, value):
MH See interfaces.IValidator
MH +context = self.context
MH field = self.field
MH -if self.context is not None:
MH -field = field.bind(self.context)
MH +widget = self.widget
MH +if context is not None:
MH +field = field.bind(context)
MH +if value is interfaces.NOT_CHANGED:
MH +if (interfaces.IContextAware.providedBy(widget) and
MH +not widget.ignoreContext):
MH +# get value from context
MH +value = zope.component.getMultiAdapter(
MH +(context, field),
MH +interfaces.IDataManager).query()
MH +else:
MH +value = interfaces.NO_VALUE
MH +if value is interfaces.NO_VALUE:
MH +# look up default value
MH +value = field.default
MH +adapter = zope.component.queryMultiAdapter(
MH +(context, self.request, self.view, field, widget),
MH +interfaces.IValue, name='default')
MH +if adapter:
MH +value = adapter.get()
MH return field.validate(value)
MH
MH def __repr__(self):
MH Modified: z3c.form/trunk/src/z3c/form/validator.txt
MH ===
MH --- z3c.form/trunk/src/z3c/form/validator.txt 2009-05-17 13:18:11 UTC
(rev 100027)
MH +++ z3c.form/trunk/src/z3c/form/validator.txt 2009-05-17 13:21:18 UTC
(rev 100028)
MH @@ -117,7 +117,6 @@
MH in the login name:
MH
MH import re
MH -
MH class LoginValidator(validator.SimpleFieldValidator):
MH...
MH... def validate(self, value):
MH @@ -182,7 +181,125 @@
MH... (None, None, None, IPerson['email'], None),
MH... interfaces.IValidator)
MH
MH +Widget Validators and File-Uploads
MH +~~
MH
MH +File-Uploads behave a bit different than the other form
MH +elements. Whether the user did not choose a file to upload
MH +``interfaces.NOT_CHANGED`` is set as value. But the validator knows
MH +how to handle this.
MH +
MH +The example has two bytes fields where File-Uploads are possible, one
MH +field is required the other