If I do not select a file and just click 'submit', I get the following error:-
Invalid pstruct: {'upload': "b'' is not a FieldStorage instance"}
This is not the behavior I get on the deform demo site where leaving it empty results in the more reasonable 'Required' error message.
Using my own validator as below does not solve the issue:-
def validate_file(node, value, **kwargs):
if not value:
raise colander.Invalid(node, "Please select a file")
class Schema(colander.MappingSchema):
excel_file = colander.SchemaNode(deform.FileData(),
widget=deform.widget.FileUploadWidget(tmpstore),
validator=validate_file)
I can see that the error is raised, but the output of e.render()
where e is the ValidationFailure
from form.validate
does not match the error itself. The relevant deform
source code is in 'widget.py' where the _FieldStorage
class checks whether cstruct
has a file
attribute and raises it's own Invalid
exception.
Here's the function which does the validation call (bog standard stuff really), which returns the rendered page.
def generate_upload_form(request):
form = deform.Form(upload_schema, buttons=('submit',))
if getattr(request, 'POST') and 'submit' in request.POST:
try:
value_dict = form.validate(request.POST.items())
except deform.ValidationFailure as e: # Invalid form
form = e.render()
else: # Successfully validated, now do operation
upload_form_operation(request, value_dict)
if isinstance(form, deform.Form):
form = form.render()
return form
How do I show my own error message without monkey-patching the deform
codebase?