Add way to set form-wide schema validator

was needed to enforce rule where one field is required only in some
cases, depending on value of another field
This commit is contained in:
Lance Edgar 2022-01-09 18:13:12 -06:00
parent 0545099a2b
commit 8579b89002

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2021 Lance Edgar
# Copyright © 2010-2022 Lance Edgar
#
# This file is part of Rattail.
#
@ -542,7 +542,10 @@ class Form(object):
# apply any validators
for key, validator in self.validators.items():
if key in schema:
if key is None:
# this one is form-wide
schema.validator = validator
elif key in schema:
schema[key].validator = validator
# apply required flags
@ -671,6 +674,17 @@ class Form(object):
self.schema[key].widget = widget
def set_validator(self, key, validator):
"""
Set the validator for the schema node represented by the given
key.
:param key: Normally this the name of one of the fields
contained in the form. It can also be ``None`` in which
case the validator pertains to the form at large instead of
one of the fields.
:param validator: Callable validator for the node.
"""
self.validators[key] = validator
def set_required(self, key, required=True):