Add validators to your template#
Validators run when a user submits a field value during the interactive prompt.
If the validator returns False, the prompt stays open and asks the user to enter a valid value.
Use a built-in validator#
Cookieplone automatically wires built-in validators to fields whose names match the DEFAULT_VALIDATORS table.
The following field names are autowired without any configuration:
Field name |
Validator applied |
|---|---|
|
|
|
|
|
|
|
|
|
|
Name your field python_package_name and the validator applies automatically.
Write a custom validator#
Place a Python module in your template's hook directory or in a package that is importable when Cookieplone runs.
A validator is any callable that takes a single string and returns True when valid and False when invalid.
def no_spaces(value: str) -> bool:
"""Reject values that contain spaces."""
return " " not in value
Reference it by its import path in your schema:
{
"validator": "myhooks.validators.no_spaces"
}