Skip to content

Commit bfb9542

Browse files
committed
Adds yet another extension point, get_form_block_class, to allow the return of subclass of a structural block for further control over the block class, such as overriding the clean() method to provide custom validation.
1 parent 9117bbb commit bfb9542

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

wagtailstreamforms/fields.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,27 @@ def get_options(self, block_value):
147147
"initial": self.get_formfield_initial(block_value),
148148
}
149149

150+
def get_form_block_class(self):
151+
"""
152+
The StreamField block class to be created for this field. This is
153+
almost always a StructBlock, but conceptually it could be any structural block.
154+
155+
Override this method and return a subclass of a structural block for further
156+
control over the block class, such as overriding the clean() method to provide
157+
custom validation.
158+
:return: The ``wagtail.blocks.StructBlock`` to be used in the StreamField
159+
"""
160+
return blocks.StructBlock
161+
150162
def get_form_block(self):
151-
"""The StreamField StructBlock.
163+
"""The StreamField block class.
152164
153165
Override this to provide additional fields in the StreamField.
154166
155-
:return: The ``wagtail.blocks.StructBlock`` to be used in the StreamField
167+
:return: The resuld of calling get_form_block_class() is to be used in the
168+
StreamField
156169
"""
157-
return blocks.StructBlock(
170+
return self.get_form_block_class()(
158171
self.get_local_blocks(),
159172
icon=self.icon,
160173
label=self.label,

0 commit comments

Comments
 (0)