Skip to content

Commit e366c16

Browse files
committed
Tidy validators.md
1 parent 781f956 commit e366c16

File tree

1 file changed

+45
-69
lines changed

1 file changed

+45
-69
lines changed
Lines changed: 45 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,98 @@
11
---
22
myst:
33
html_meta:
4-
"description": ""
5-
"property=og:description": ""
6-
"property=og:title": ""
7-
"keywords": ""
4+
"description": "Validators"
5+
"property=og:description": "Validators"
6+
"property=og:title": "Validators"
7+
"keywords": "Plone, Validators, constraints, content types"
88
---
99

1010
# Validators
1111

12-
**Creating custom validators for your type**
12+
This chapter describes how to create custom validators for your type.
1313

14-
Many applications require some form of data entry validation. The
15-
simplest form of validation you get for free – the [z3c.form] library
16-
ensures that all data entered on Dexterity add and edit forms is valid
17-
for the field type.
14+
Many applications require some form of data entry validation.
15+
The simplest form of validation you get for free, the [z3c.form](https://pypi.org/project/z3c.form/) library ensures that all data entered in Dexterity add and edit forms is valid for the field type.
1816

19-
It is also possible to set certain properties on the fields to add
20-
further validation (or even create your own fields with custom
21-
validation logic, although that is a lot less common). These properties
22-
are set as parameters to the field constructor when the schema interface
23-
is created. You should see the [zope.schema] package for details, but
24-
the most common constraints are:
17+
It is also possible to set certain properties on the fields to add further validation, or even create your own fields with custom validation logic, although that is a lot less common.
18+
These properties are set as parameters to the field constructor when the schema interface is created.
19+
You should see the [zope.schema](https://pypi.org/project/zope.schema/) package for details.
2520

26-
`required=True/False`
21+
The most common constraints are:
2722

23+
`required=True/False`
2824
: to make a field required or optional;
2925

3026
`min` and `max`
31-
32-
: used for `Int`, `Float`, `Datetime`, `Date`, and `Timedelta`
33-
fields, specify the minimum and maximum (inclusive) allowed values of
34-
the given type;
27+
: used for `Int`, `Float`, `Datetime`, `Date`, and `Timedelta` fields, specify the minimum and maximum (inclusive) allowed values of the given type.
3528

3629
`min_length` and `max_length`
30+
: used for collection fields (`Tuple`, `List`, `Set`, `Frozenset`, `Dict`) and text fields (`Bytes`, `BytesLine`, `ASCII`, `ASCIILine`, `Text`, `TextLine`), set the minimum and maximum (inclusive) length of a field.
3731

38-
: used for collection fields (`Tuple`, `List`, `Set`, `Frozenset`,
39-
`Dict`) and text fields (`Bytes`, `BytesLine`, `ASCII`,
40-
`ASCIILine`, `Text`, `TextLine`), set the minimum and maximum
41-
(inclusive) length of a field.
4232

4333
## Constraints
4434

45-
If this does not suffice, you can pass your own constraint function to a
46-
field. The constraint function should take a single argument: the value
47-
that is to be validated. This will be of the fields type. The function
48-
should return a boolean `True` or `False`.
35+
If this does not suffice, you can pass your own constraint function to a field.
36+
The constraint function should take a single argument: the value that is to be validated.
37+
This will be the field's type.
38+
The function should return a boolean `True` or `False`.
4939

50-
```
40+
```python
5141
def checkForMagic(value):
5242
return 'magic' in value
5343
```
5444

55-
:::{note}
56-
Hint: The constraint function does not have access to the context, but
57-
if you need to acquire a tool, you can use the
58-
`zope.component.hooks.getSite()` method to obtain the site root.
59-
:::
45+
```{note}
46+
The constraint function does not have access to the context, but if you need to acquire a tool, you can use the `zope.component.hooks.getSite()` method to obtain the site root.
47+
```
6048

61-
To use the constraint, pass the function as the `constraint` argument to
62-
the field constructor, e.g.:
49+
To use the constraint, pass the function as the `constraint` argument to the field constructor, for example:
6350

64-
```
51+
```python
6552
my_field = schema.TextLine(title=_(u"My field"), constraint=checkForMagic)
6653
```
6754

68-
Constraints are easy to write, but do not necessarily produce very
69-
friendly error messages. It is however possible to customise these error
70-
messages using [z3c.form] error view snippets. See the [z3c.form
71-
documentation](z3c.form) for more details.
55+
Constraints are easy to write, but do not necessarily produce very friendly error messages.
56+
It is possible to customize these error messages using `z3c.form` error view snippets.
57+
See the [z3c.form documentation](https://z3cform.readthedocs.io/en/latest/) for more details.
7258

73-
## Invariants
7459

75-
You’ll also notice that constraints only check a single field value. If
76-
you need to write a validator that compares multiple values, you can use
77-
an invariant. Invariants use exceptions to signal errors, which are
78-
displayed at the top of the form rather than next to a particular field.
60+
## Invariants
7961

80-
To illustrate an invariant, let’s make sure that the start date of a
81-
`Program` is before the end date. In `program.py`, we add the following.
82-
Code not relevant to this example is snipped with an ellipsis (…):
62+
You'll also notice that constraints only check a single field value.
63+
If you need to write a validator that compares multiple values, you can use an invariant.
64+
Invariants use exceptions to signal errors, which are displayed at the top of the form rather than next to a particular field.
8365

84-
```
85-
...
66+
To illustrate an invariant, let's make sure that the start date of a `Program` is before the end date.
67+
In `program.py`, we add the following.
8668

69+
```python
8770
from zope.interface import invariant, Invalid
8871

8972
class StartBeforeEnd(Invalid):
90-
__doc__ = _(u"The start or end date is invalid")
73+
__doc__ = _("The start or end date is invalid")
9174

9275
class IProgram(model.Schema):
9376

94-
...
95-
9677
start = schema.Datetime(
97-
title=_(u"Start date"),
98-
required=False,
99-
)
78+
title=_("Start date"),
79+
required=False,
80+
)
10081

10182
end = schema.Datetime(
102-
title=_(u"End date"),
103-
required=False,
104-
)
105-
106-
...
83+
title=_("End date"),
84+
required=False,
85+
)
10786

10887
@invariant
10988
def validateStartEnd(data):
11089
if data.start is not None and data.end is not None:
11190
if data.start > data.end:
112-
raise StartBeforeEnd(_(u"The start date must be before the end date."))
113-
114-
...
91+
raise StartBeforeEnd(_("The start date must be before the end date."))
11592
```
11693

117-
## Form validators
11894

119-
Finally, you can write more powerful validators by using the [z3c.form](http://pypi.python.org/pypi/z3c.form)
120-
widget validators. For details see the [z3c.form validators section](http://docs.plone.org/develop/plone/forms/z3c.form.html#validators).
95+
## Form validators
12196

122-
[zope.schema]: http://pypi.python.org/pypi/zope.schema
97+
Finally, you can write more powerful validators by using the `z3c.form` widget validators.
98+
For details see [`z3c.form` validators](https://5.docs.plone.org/develop/plone/forms/z3c.form.html#validators).

0 commit comments

Comments
 (0)