I have a validator example here for our DGF (in a separate group form (=fieldset)). The versions are for Plone 6 but this should work on older DGF too:
from plone.autoform import directives
from plone.supermodel import model
from z3c.form import validator
from zope import schema
from zope.interface import Interface
class IDGFSchema(Interface):
last_name = schema.TextLine(title=_("last_name"))
first_name = schema.TextLine(title=_("first_name"))
birthday = schema.Date(title=_("birthday"))
disability = schema.Bool(title=_("fp_children_disability"), required=False)
directives.widget(
"birthday",
DateFieldWidget,
_formater_length="long",
)
class IContentSchema(model.Schema):
children = schema.List(
title=_("fp_children"),
value_type=DictRow(
title="child",
schema=IDGFSchema,
),
default=[],
required=False,
)
directives.widget(
"children",
DataGridFieldWidgetFactory,
auto_append=False,
input_table_css_class="table table-sm",
display_table_css_class="table table-sm",
)
model.fieldset(
"children",
label=_("Children"),
fields=[
"children",
],
)
class ChildrenValidator(validator.SimpleFieldValidator):
def validate(self, value):
if value is None:
value = []
_valid = bool(len(value))
_msg = _("Invalid children data")
for c in value:
if not c["first_name"] or not c["last_name"] or not c["birthday"]:
_valid = False
continue
try:
b_year = c["birthday"].year
if (
b_year <= (datetime.now().year - CHILD_AGE_LIMIT)
and not c["disability"]
):
_valid = False
_msg = _(
"Too old. Minimum birthyear: ${min_year}",
mapping=dict(min_year=datetime.now().year - CHILD_AGE_LIMIT + 1),
)
except Exception:
pass
if not _valid:
raise Invalid(_msg)
validator.WidgetValidatorDiscriminators(
ChildrenValidator,
field=IContentSchema["children"],
)
and register the ChildrenValidator
adapter in a zcml file:
<adapter factory=".mymodule.ChildrenValidator" />