@view_config(renderer="templates/form.pt", name="require_one_or_another")
@demonstrate("Require One Field or Another")
def require_one_or_another(self):
class Schema(colander.Schema):
one = colander.SchemaNode(
colander.String(),
missing=unicode(""),
title="One (required if Two is not supplied)",
)
two = colander.SchemaNode(
colander.String(),
missing=unicode(""),
title="Two (required if One is not supplied)",
)
def validator(form, value):
if not value["one"] and not value["two"]:
exc = colander.Invalid(
form, 'A value for either "one" or "two" is required'
)
exc["one"] = "Required if two is not supplied"
exc["two"] = "Required if one is not supplied"
raise exc
schema = Schema(validator=validator)
form = deform.Form(schema, buttons=("submit",))
return self.render_form(form)