validationpyramidmakodeform

Adding a deform form in an existing page (mako template) validator not called?


I have an existing (WIP) pyramid project, with the simplistic forms all being done by hand. As the user requirements have been steadily increasing in complexity, I wanted to integrate deform forms to simplify my own maintenance/programming tasks.

My initial test was to try for an interfield form[1], the purpose being to ensure that a certain date predates another date in the form. Here's the simplified definition for the schema and validator:-

class Schema(colander.MappingSchema):
    startdate = colander.SchemaNode(colander.Date())
    enddate = colander.SchemaNode(colander.Date())
def validator(form, value):
    if value['enddate'] - value['startdate'] < 0:
        exc = colander.Invalid(form, 'Start date must precede End date')
        exc['enddate'] = 'Must be after %s' % value['startdate']
        raise exc

schema = Schema(validator=validator)
form = deform.Form(schema, buttons=('submit',))

I then pass the form to my mako template and call:-

${form.render() | n}

This renders the form properly, and my date selectors work (of course, after I had to mess around with loading the correct CSS and javascripts). However clicking submit doesn't do any validation (not even the basic 'you didn't enter a value'), instead it goes right back to my view_config.

What could I be missing?

[1] - https://deformdemo.pylonsproject.org/interfield/


Solution

  • It turns out deform doesn't handle the validation automatically, and I have to actually call validate, something like below:-

    try:
        appstruct = form.validate(request.POST.items())
    except deform.ValidationFailure as e:
        return {'form': e.render()}