Skip to content

Commit 22c9adb

Browse files
committed
Add strings fields documentation
1 parent 29d61a3 commit 22c9adb

File tree

1 file changed

+220
-4
lines changed

1 file changed

+220
-4
lines changed

docs/forms.md

Lines changed: 220 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ For all fields, processed by Flask-Mongoengine integration:
4848
{attr}`validators` extension by Flask-Mongoengine.
4949
- If model field definition have {attr}`wtf_filters` defined, they will be forwarded to
5050
WTForm as {attr}`filters`.
51-
- If model field definition have {attr}`required`, then {class}`~wtforms.validators.InputRequired`
52-
will be added to form {attr}`validators`, otherwise {class}`~wtforms.validators.Optional`
51+
- If model field definition have {attr}`required`, then
52+
{class}`~wtforms.validators.InputRequired`
53+
will be added to form {attr}`validators`, otherwise
54+
{class}`~wtforms.validators.Optional`
5355
added.
5456
- If model field definition have {attr}`verbose_name` it will be used as form field
5557
{attr}`label`, otherwise pure field name used.
@@ -103,7 +105,53 @@ Not yet documented. Please help us with new pull request.
103105

104106
### EmailField
105107

106-
Not yet documented. Please help us with new pull request.
108+
- API: {class}`.db_fields.EmailField`
109+
- Default form field class: {class}`~.MongoEmailField`
110+
111+
#### Form generation behaviour
112+
113+
Unlike [StringField] WTForm class of the field is not adjusted by normal form
114+
generation sequence and always match {class}`~.MongoEmailField`. All other
115+
adjustments, related to validators insert are work with EmailField in the same way,
116+
as in [StringField].
117+
118+
Additional {class}`~wtforms.validators.Email` validator is also inserted to form
119+
field, to exclude unnecessary database request, if form data incorrect.
120+
121+
Field respect user's adjustments in {attr}`wtf_field_class` option of
122+
{class}`.db_fields.EmailField`. This will change form field display, but will not
123+
change inserted validators.
124+
125+
#### Examples
126+
127+
strings_demo.py in example app contain basic non-requirement example. You can adjust
128+
it to any provided example for test purposes.
129+
130+
##### Not required EmailField
131+
132+
```python
133+
"""strings_demo.py"""
134+
from example_app.models import db
135+
136+
137+
class StringsDemoModel(db.Document):
138+
"""Documentation example model."""
139+
140+
url_field = db.EmailField()
141+
````
142+
143+
##### Required EmailField
144+
145+
```python
146+
"""strings_demo.py"""
147+
from example_app.models import db
148+
149+
150+
class StringsDemoModel(db.Document):
151+
"""Documentation example model."""
152+
153+
required_url_field = db.EmailField(required=True)
154+
````
107155

108156
### EmbeddedDocumentField
109157

@@ -181,9 +229,169 @@ in another database fields too, but all of them based on
181229
- {class}`~.MongoTextAreaField`
182230
- {class}`~.MongoURLField`
183231

232+
#### Examples
233+
234+
strings_demo.py in example app contain basic non-requirement example. You can adjust
235+
it to any provided example for test purposes.
236+
237+
##### Not limited StringField as MongoTextAreaField
238+
239+
```python
240+
"""strings_demo.py"""
241+
from example_app.models import db
242+
243+
244+
class StringsDemoModel(db.Document):
245+
"""Documentation example model."""
246+
247+
string_field = db.StringField()
248+
```
249+
250+
##### Not limited StringField as MongoTelField
251+
252+
```python
253+
"""strings_demo.py"""
254+
from example_app.models import db
255+
from flask_mongoengine.wtf import fields as mongo_fields
256+
257+
258+
class StringsDemoModel(db.Document):
259+
"""Documentation example model."""
260+
261+
tel_field = db.StringField(wtf_field_class=mongo_fields.MongoTelField)
262+
```
263+
264+
##### Not limited StringField as MongoTextAreaField with https regex
265+
266+
[mongoengine] and [wtforms] projects are not consistent in how they work with regex.
267+
You will be safe, if you use {func}`re.compile` each time, when you work with regex
268+
settings, before parent projects itself.
269+
270+
```python
271+
"""strings_demo.py"""
272+
import re
273+
274+
from example_app.models import db
275+
276+
277+
class StringsDemoModel(db.Document):
278+
"""Documentation example model."""
279+
280+
regexp_string_field = db.StringField(regex=re.compile(
281+
r"^(https:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=]+$"
282+
))
283+
```
284+
285+
##### Size limited StringField as MongoStringField
286+
287+
```python
288+
"""strings_demo.py"""
289+
from example_app.models import db
290+
291+
292+
class StringsDemoModel(db.Document):
293+
"""Documentation example model."""
294+
295+
sized_string_field = db.StringField(min_length=5)
296+
```
297+
298+
##### Required password field with minimum size
299+
300+
```python
301+
"""strings_demo.py"""
302+
from example_app.models import db
303+
from flask_mongoengine.wtf import fields as mongo_fields
304+
305+
306+
class StringsDemoModel(db.Document):
307+
"""Documentation example model."""
308+
309+
password_field = db.StringField(
310+
wtf_field_class=mongo_fields.MongoPasswordField,
311+
required=True,
312+
min_length=5,
313+
)
314+
```
315+
184316
### URLField
185317

186-
Not yet documented. Please help us with new pull request.
318+
- API: {class}`.db_fields.URLField`
319+
- Default form field class: {class}`~.MongoURLField`
320+
321+
#### Form generation behaviour
322+
323+
Unlike [StringField] WTForm class of the field is not adjusted by normal form
324+
generation sequence and always match {class}`~.MongoURLField`. All other
325+
adjustments, related to validators insert are work with EmailField in the same way,
326+
as in [StringField].
327+
328+
Additional {class}`~wtforms.validators.Regexp` validator is also inserted to form
329+
field, to exclude unnecessary database request, if form data incorrect. This
330+
validator use regexp, provided in {attr}`url_regex` of {class}`.db_fields.URLField`,
331+
or default URL regexp from [mongoengine] project. This is different from
332+
Flask-Mongoengine version **1.0.0** or earlier, where {class}`~wtforms.validators.URL`
333+
was inserted. This was changed, to exclude validators conflicts.
334+
335+
```{important}
336+
{func}`~.model_form` is still use {class}`~wtforms.validators.URL` for
337+
compatibility with old setups.
338+
```
339+
340+
Field respect user's adjustments in {attr}`wtf_field_class` option of
341+
{class}`.db_fields.URLField`. This will change form field display, but will not
342+
change inserted validators.
343+
344+
#### Examples
345+
346+
strings_demo.py in example app contain basic non-requirement example. You can adjust
347+
it to any provided example for test purposes.
348+
349+
##### Not required URLField
350+
351+
```python
352+
"""strings_demo.py"""
353+
from example_app.models import db
354+
355+
356+
class StringsDemoModel(db.Document):
357+
"""Documentation example model."""
358+
359+
url_field = db.URLField()
360+
````
361+
362+
##### Required URLField with minimum size
363+
364+
```python
365+
"""strings_demo.py"""
366+
from example_app.models import db
367+
368+
369+
class StringsDemoModel(db.Document):
370+
"""Documentation example model."""
371+
372+
required_url_field = db.URLField(required=True, min_length=25)
373+
````
374+
375+
##### URLField with https only regexp check, if data exist
376+
377+
Regexp for {attr}`url_regex` should be prepared by {mod}`re`.
378+
379+
```python
380+
"""strings_demo.py"""
381+
import re
382+
383+
from example_app.models import db
384+
385+
386+
class StringsDemoModel(db.Document):
387+
"""Documentation example model."""
388+
389+
https_url_field = db.URLField(
390+
url_regex=re.compile(
391+
r"^(https:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=]+$"
392+
),
393+
)
394+
````
187395

188396
## Unsupported fields
189397

@@ -282,7 +490,15 @@ Not yet documented. Please help us with new pull request.
282490
Not yet documented. Please help us with new pull request.
283491

284492
[mongoengine]: https://docs.mongoengine.org/
493+
285494
[supported fields]: #supported-fields
495+
286496
[#379]: https://github.com/MongoEngine/flask-mongoengine/issues/379
497+
287498
[integration]: forms
499+
288500
[global transforms]: #global-transforms
501+
502+
[stringfield]: #stringfield
503+
504+
[wtforms]: https://wtforms.readthedocs.io/en/3.0.x/

0 commit comments

Comments
 (0)