From 1ee822d71538d51255a6eadf3fc5c596299d5871 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 24 Oct 2017 19:58:59 -0700 Subject: [PATCH] Add support for validator and required flag, for new forms --- tailbone/forms2/core.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/tailbone/forms2/core.py b/tailbone/forms2/core.py index d7ad0e5a..5d7148c2 100644 --- a/tailbone/forms2/core.py +++ b/tailbone/forms2/core.py @@ -178,7 +178,7 @@ class Form(object): def __init__(self, fields=None, schema=None, request=None, readonly=False, readonly_fields=[], model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={}, - widgets={}, defaults={}, action_url=None, cancel_url=None): + widgets={}, defaults={}, validators={}, required={}, action_url=None, cancel_url=None): self.fields = list(fields) if fields is not None else None self.schema = schema @@ -197,6 +197,8 @@ class Form(object): self.renderers = renderers or {} self.widgets = widgets or {} self.defaults = defaults or {} + self.validators = validators or {} + self.required = required or {} self.action_url = action_url self.cancel_url = cancel_url @@ -271,6 +273,19 @@ class Form(object): if key in schema: schema[key].default = default + # apply any validators + for key, validator in self.validators.items(): + if key in schema: + schema[key].validator = validator + + # apply required flags + for key, required in self.required.items(): + if key in schema: + if required: + schema[key].missing = colander.required + else: + schema[key].missing = None # TODO? + self.schema = schema return self.schema @@ -325,14 +340,13 @@ class Form(object): self.widgets[key] = widget def set_validator(self, key, validator): - schema = self.make_schema() - schema[key].validator = validator + self.validators[key] = validator - def set_required(self, key, required): + def set_required(self, key, required=True): """ Set whether or not value is required for a given field. """ - raise NotImplementedError # TODO + self.required[key] = required def set_default(self, key, value): """