Add support for validator and required flag, for new forms
This commit is contained in:
parent
8bcb2f750a
commit
1ee822d715
|
@ -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):
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue