|
1 | 1 | --- |
2 | 2 | myst: |
3 | 3 | 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" |
8 | 8 | --- |
9 | 9 |
|
10 | 10 | # Validators |
11 | 11 |
|
12 | | -**Creating custom validators for your type** |
| 12 | +This chapter describes how to create custom validators for your type. |
13 | 13 |
|
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. |
18 | 16 |
|
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. |
25 | 20 |
|
26 | | -`required=True/False` |
| 21 | +The most common constraints are: |
27 | 22 |
|
| 23 | +`required=True/False` |
28 | 24 | : to make a field required or optional; |
29 | 25 |
|
30 | 26 | `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. |
35 | 28 |
|
36 | 29 | `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. |
37 | 31 |
|
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. |
42 | 32 |
|
43 | 33 | ## Constraints |
44 | 34 |
|
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 field’s 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`. |
49 | 39 |
|
50 | | -``` |
| 40 | +```python |
51 | 41 | def checkForMagic(value): |
52 | 42 | return 'magic' in value |
53 | 43 | ``` |
54 | 44 |
|
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 | +``` |
60 | 48 |
|
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: |
63 | 50 |
|
64 | | -``` |
| 51 | +```python |
65 | 52 | my_field = schema.TextLine(title=_(u"My field"), constraint=checkForMagic) |
66 | 53 | ``` |
67 | 54 |
|
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. |
72 | 58 |
|
73 | | -## Invariants |
74 | 59 |
|
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 |
79 | 61 |
|
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. |
83 | 65 |
|
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. |
86 | 68 |
|
| 69 | +```python |
87 | 70 | from zope.interface import invariant, Invalid |
88 | 71 |
|
89 | 72 | class StartBeforeEnd(Invalid): |
90 | | - __doc__ = _(u"The start or end date is invalid") |
| 73 | + __doc__ = _("The start or end date is invalid") |
91 | 74 |
|
92 | 75 | class IProgram(model.Schema): |
93 | 76 |
|
94 | | - ... |
95 | | -
|
96 | 77 | start = schema.Datetime( |
97 | | - title=_(u"Start date"), |
98 | | - required=False, |
99 | | - ) |
| 78 | + title=_("Start date"), |
| 79 | + required=False, |
| 80 | + ) |
100 | 81 |
|
101 | 82 | end = schema.Datetime( |
102 | | - title=_(u"End date"), |
103 | | - required=False, |
104 | | - ) |
105 | | -
|
106 | | - ... |
| 83 | + title=_("End date"), |
| 84 | + required=False, |
| 85 | + ) |
107 | 86 |
|
108 | 87 | @invariant |
109 | 88 | def validateStartEnd(data): |
110 | 89 | if data.start is not None and data.end is not None: |
111 | 90 | 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.")) |
115 | 92 | ``` |
116 | 93 |
|
117 | | -## Form validators |
118 | 94 |
|
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 |
121 | 96 |
|
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