Inter-Field Validation

Captured

None

Code (show in context)

    @view_config(renderer='templates/form.pt', name='interfield')
    @demonstrate('Inter-Field Validation')
    def interfield(self):
        class Schema(colander.Schema):
            name = colander.SchemaNode(
                colander.String(),
                description='Content name')
            title = colander.SchemaNode(
                colander.String(),
                description='Content title (must start with content name)')
        def validator(form, value):
            if not value['title'].startswith(value['name']):
                exc = colander.Invalid(form, 'Title must start with name')
                exc['title'] = 'Must start with name %s' % value['name']
                raise exc
        schema = Schema(validator=validator)
        form = deform.Form(schema, buttons=('submit',))
        return self.render_form(form)