Skip to content

Commit 11e7da7

Browse files
committed
style cloning and better docs, thanks tclerckx
1 parent f65ce73 commit 11e7da7

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

docs/chapter-07.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,9 @@ only for fields of type “string”. ``uploadfield``, ``authorize``, and
10471047
- ``authorize`` can be used to require access control on the
10481048
corresponding field, for “upload” fields only. It will be discussed
10491049
more in detail in the context of Authentication and Authorization.
1050-
- ``widget`` do not use DAL widgets with py4web, it has has its own
1051-
(see :ref:`Widgets` later).
1050+
- ``widget`` Do NOT use the widget parameter in py4web for a Field definition.
1051+
(This was a feature of web2py and is not to be used in py4web)
1052+
See :ref:`Widgets` later.
10521053
- ``represent`` can be None or can point to a function that takes a
10531054
field value and returns an alternate representation for the field
10541055
value.

docs/chapter-12.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ Where:
100100
- ``readonly``: set to True to make a readonly form
101101
- ``deletable``: set to False to disallow deletion of record
102102
- ``formstyle``: a function that renders the form using helpers.
103-
Can be FormStyleDefault (default), FormStyleBulma,
104-
FormStyleBootstrap4, or FormStyleBootstrap5
103+
Can be ``FormStyleDefault`` (default), ``FormStyleBulma``,
104+
``FormStyleBootstrap4``, or ``FormStyleBootstrap5``.
105105
- ``dbio``: set to False to prevent any DB writes
106106
- ``keep_values``: if set to true, it remembers the values of the previously submitted form
107107
- ``form_name``: the optional name of this form
@@ -111,6 +111,13 @@ Where:
111111
- ``lifespan``: lifespan of CSRF token in seconds, to limit form validity
112112
- ``signing_info``: information that should not change between when the CSRF token is signed and verified
113113

114+
``FormStyleDefault`` is an object that is used by default for every form
115+
and it is defined in ``py4web.utils.form.FormStyleDefault``.
116+
You should never change it but you can make a copy and pass it as ``formstyle``.
117+
118+
You can change the style of a field named, for example, ``color``
119+
by changing the attribute ``color`` of the FormStyle.
120+
114121

115122
A minimal form example without a database
116123
-----------------------------------------
@@ -352,8 +359,8 @@ Using widgets in forms is quite easy, and they'll let you have more control on i
352359
Custom widgets
353360
~~~~~~~~~~~~~~
354361

355-
You can also customize the widgets properties by subclassing the FormStyleDefault class. Let's have a quick look,
356-
improving again our Superhero example:
362+
You can also customize the widgets properties by cloning an modifying and existing style.
363+
Let's have a quick look, improving again our Superhero example:
357364

358365
.. code:: python
359366
@@ -383,7 +390,7 @@ improving again our Superhero example:
383390
@action("create_form", method=["GET", "POST"])
384391
@action.uses("form_custom_widgets.html", db)
385392
def create_form():
386-
MyStyle = FormStyleDefault
393+
MyStyle = FormStyleDefault.clone()
387394
MyStyle.classes = FormStyleDefault.classes
388395
MyStyle.widgets['name']=MyCustomWidget()
389396
MyStyle.widgets['color']=RadioWidget()

py4web/utils/form.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ def __init__(self):
280280
self.class_inner_exceptions = copy.copy(self._class_inner_exceptions)
281281
self.widgets = copy.copy(self._widgets)
282282

283+
def clone(self):
284+
return copy.deepcopy(self)
285+
283286
def __call__(
284287
self,
285288
table,

0 commit comments

Comments
 (0)