Skip to content

Commit 829ea20

Browse files
committed
apply changes proposed by @stevepiercy
1 parent 054e9e1 commit 829ea20

File tree

1 file changed

+73
-37
lines changed

1 file changed

+73
-37
lines changed

docs/backend/widgets.md

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ This model is defined as a `zope.schema` based schema, but extra hints can be su
7070

7171
By default, `z3c.form` picks a widget based on the type of your field.
7272
You can change the widget using the `widget` directive if you want users to enter or view data in a different format.
73-
For example, you can change the widget for the `human` boolean field to use "yes" and "no" radio buttons instead of its default checkbox:
73+
For example, you can change the widget for the `human` ``boolean`` field to use "yes" and "no" radio buttons instead of its default checkbox:
7474

7575
```{code-block} python
7676
:emphasize-lines: 7
@@ -211,7 +211,9 @@ Before that, the `groups` attribute is just a list of group factories.
211211
During the update method though, the groups have been initialized and have their own widget list each.
212212
To access widgets in a group, you have to access the group in the update method:
213213

214-
```python
214+
``` {code-block} python
215+
:linenos:
216+
215217
from z3c.form import form
216218
217219
@@ -229,7 +231,9 @@ class MyForm(form.Form):
229231
You can customize widget options in the `updateWidgets()` method.
230232
Note that `fieldset` (which is a group) is a `subform`, and this method only affects the current `fieldset`.
231233

232-
```python
234+
``` {code-block} python
235+
:linenos:
236+
233237
from z3c.form import form
234238
235239
@@ -250,7 +254,9 @@ class MyForm(form.Form):
250254

251255
With `plone.z3cform`, you can reorder the field widgets by overriding the `update()` method of the form class.
252256

253-
```python
257+
``` {code-block} python
258+
:linenos:
259+
254260
from z3c.form import form
255261
from z3c.form.interfaces import HIDDEN_MODE
256262
from plone.z3cform.fieldsets.utils import move
@@ -271,7 +277,9 @@ class MyForm(form.Form):
271277

272278
You also can use `plone.autoform` directives, as in this example used for forms:
273279

274-
```python
280+
``` {code-block} python
281+
:linenos:
282+
275283
from plone.autoform import directives as form
276284
from z3c.form.interfaces import IAddForm, IEditForm
277285
@@ -300,7 +308,9 @@ class IFlexibleContent(form.Schema):
300308
Sometimes you need to pre-load widget values to show when the form is requested.
301309
The following example shows how to do that.
302310

303-
```python
311+
``` {code-block} python
312+
:linenos:
313+
304314
from z3c.form import field
305315
from z3c.form import form
306316
from z3c.form.browser.checkbox import CheckBoxFieldWidget
@@ -340,7 +350,9 @@ This will result in a checked option value of `02`.
340350

341351
The following example shows how you can conditionally require widgets.
342352

343-
```python
353+
``` {code-block} python
354+
:linenos:
355+
344356
class ReportForm(form.Form):
345357
""" A form to output an HTML report from chosen parameters """
346358
@@ -358,13 +370,17 @@ class ReportForm(form.Form):
358370
To add CSS classes to a widget, you can use the method `addClass()`.
359371
This is useful when you have JavaScript associated with your form:
360372

361-
```python
373+
``` {code-block} python
374+
:linenos:
375+
362376
widget.addClass("myspecialwidgetclass")
363377
```
364378

365379
You can also override the widget CSS class by changing the `klass` attribute for a given widget:
366380

367-
```python
381+
``` {code-block} python
382+
:linenos:
383+
368384
self.widgets["my_widget"].klass = "my-custom-css-class"
369385
```
370386

@@ -452,7 +468,9 @@ The following code is an example of a custom template `yourwidget.pt` for an `in
452468

453469
Now you can override the template factory in the `updateWidgets()` method of your form class.
454470

455-
```python
471+
``` {code-block} python
472+
:linenos:
473+
456474
from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile as Z3ViewPageTemplateFile
457475
from z3c.form.interfaces import INPUT_MODE
458476
@@ -472,7 +490,7 @@ class AddForm(DefaultAddForm):
472490
473491
# widget.template is a template factory -
474492
# Widget.render() will associate later this factory with the widget
475-
widget.template = Z3ViewPageTemplateFile("templates/sections.pt")
493+
widget.template = Z3ViewPageTemplateFile("templates/yourwidget.pt")
476494
```
477495

478496
You can also interact with your `form` class instance from the widget template:
@@ -509,24 +527,26 @@ You can override widget templates as instructed for `z3c.form`.
509527
You might want to customize this widget frame for your own form.
510528
Below is an example of how to do it.
511529

512-
Copy [`widget.pt`](https://github.com/plone/plone.app.z3cform/blob/master/plone/app/z3cform/templates/widget.pt) to your own package, and edit it.
530+
Copy [`widget.pt`](https://github.com/plone/plone.app.z3cform/blob/master/plone/app/z3cform/templates/widget.pt) to your own package, rename it as ``demo-widget.pt`` and edit it.
513531

514-
Then add the following code to `configure.zcml`.
532+
Then add the following code to `configure.zcml`. Remember to fix the path of the template according to your own paths.
515533

516534
```xml
517535
<browser:page
518536
name="ploneform-render-widget"
519537
for=".demo.IDemoWidget"
520538
class="plone.app.z3cform.templates.RenderWidget"
521539
permission="zope.Public"
522-
template="demo-widget.pt"
540+
template="path/to/template/demo-widget.pt"
523541
/>
524542

525543
```
526544

527545
Then create a new marker interface in Python code.
528546

529-
```python
547+
``` {code-block} python
548+
:linenos:
549+
530550
from zope.interface import Interface
531551
532552
@@ -536,7 +556,9 @@ class IDemoWidget(Interface):
536556

537557
Then apply this marker interface to your widgets in `form.update()`.
538558

539-
```python
559+
``` {code-block} python
560+
:linenos:
561+
540562
from zope.interface import alsoProvides
541563
542564
class MyForm(...):
@@ -555,7 +577,9 @@ You can combine several widgets into one with `z3c.form.browser.multil.MultiWidg
555577

556578
The following example shows how to create an input widget with minimum and maximum values.
557579

558-
```python
580+
``` {code-block} python
581+
:linenos:
582+
559583
import zope.interface
560584
import zope.schema
561585
from zope.schema.fieldproperty import FieldProperty
@@ -582,34 +606,46 @@ class MinMax(object):
582606
registerFactoryAdapter(IMinMax, MinMax)
583607
584608
585-
field = zope.schema.Object(
586-
__name__="minmax",
587-
title=label,
588-
schema=IMinMax,
589-
required=False
590-
)
609+
class IMyFormSchema(zope.interface.Interface):
610+
611+
my_combined_field = zope.schema.Object(
612+
__name__="minmax",
613+
title=label,
614+
schema=IMinMax,
615+
required=False
616+
)
617+
591618
```
592619

593-
Then you mark the widget in `updateWidgets()`:
620+
Then you set the {guilabel}`my_combined_field` widget template in `updateWidgets()`:
594621

595-
```python
596-
def updateWidgets(self):
597-
"""
598-
"""
622+
``` {code-block} python
623+
:emphasize-lines: 13, 14
624+
:linenos:
625+
626+
class MyForm(form.Form):
627+
628+
fields = field.Fields(IMyFormSchema)
629+
630+
def updateWidgets(self, prefix=None):
631+
"""
632+
"""
599633
600-
super(FilteringGroup, self).updateWidgets()
634+
super().updateWidgets(prefix=prefix)
601635
602-
# Add min and max CSS class rendering hints
603-
for widget in self.widgets.values():
604-
if isinstance(widget, z3c.form.browser.object.ObjectWidget):
605-
widget.template = Z3ViewPageTemplateFile("templates/minmax.pt")
606-
widget.addClass("min-max-widget")
607-
zope.interface.alsoProvides(widget, IFilterWidget)
636+
# Add min and max CSS class rendering hints
637+
for widget in self.widgets.values():
638+
if isinstance(widget, z3c.form.browser.object.ObjectWidget):
639+
widget.template = Z3ViewPageTemplateFile("minmax.pt")
640+
zope.interface.alsoProvides(widget, IFilterWidget)
608641
```
609642

610-
And then the page template which renders both 0. widget (min) and 1. widget (max) on the same line.
643+
Then create the page template `minmax.pt` in the same directory as your form module.
644+
Paste the following code in this file.
645+
The code renders both widgets, {guilabel}`min` and {guilabel}`max`, in a single row.
646+
647+
```html
611648

612-
``` {code-block} html
613649
<div class="min-max-widget"
614650
tal:define="widget0 python:view.subform.widgets.values()[0];
615651
widget1 python:view.subform.widgets.values()[1];">

0 commit comments

Comments
 (0)