My use case: in a setuphandler i iterate over all DX-Type defintions and add a behavior. That's ok and do what i need:
# setuphandlers.py
def add_taxonomy_to_fti(context):
taxonomy = schema.Set(
title=_("taxonomy_studiengang"),
description=_("Select Course and Semester"),
required=False,
value_type=schema.Choice(
vocabulary="collective.taxonomy.taxonomies",
),
)
portal = api.portal.get()
portal_types = portal.portal_types
_behavior = "collective.taxonomy.generated.studienablauf"
for dxtype in TYPES_FOR_TAXONOMY:
fti = portal_types.get(dxtype)
if _behavior in list(fti.behaviors):
continue
fti.behaviors = fti.behaviors + (_behavior,)
typeschema = fti.lookupSchema()
schemaeditor = IEditableSchema(typeschema)
fieldname = "taxonomy_studienablauf"
try:
schemaeditor.removeField(fieldname)
except ValueError:
logger.info(
"Fieldname for Taxonomy not found {} {}".format(dxtype, fieldname)
)
finally:
notify(FieldRemovedEvent(fti, taxonomy))
notify(SchemaInvalidatedEvent(portal_type=dxtype))
schemaeditor.addField(
taxonomy,
name=fieldname,
)
notify(ObjectAddedEvent(taxonomy, typeschema))
notify(FieldAddedEvent(fti, taxonomy))
notify(SchemaInvalidatedEvent(portal_type=dxtype))
# validators.py
from collective.taxonomy.interfaces import ITaxonomySelectWidget
from z3c.form import validator
from z3c.form.interfaces import IOrderedSelectWidget
from z3c.form.validator import SimpleFieldValidator
from zope.component import provideAdapter
from zope.interface import Invalid
class CourseOfStudiesValidator(SimpleFieldValidator):
"""z3c.form validator class for studiumsablauf taxonomy"""
def validate(self, value):
print("in my validation method")
"""Validate selected items count from the list, should be greater zero"""
super(CourseOfStudiesValidator, self).validate(value)
if not value:
raise Invalid("My custom error message")
validator.WidgetValidatorDiscriminators(
CourseOfStudiesValidator, widget=IOrderedSelectWidget
)
validator.WidgetValidatorDiscriminators(
CourseOfStudiesValidator, widget=ITaxonomySelectWidget
)
provideAdapter(CourseOfStudiesValidator)
My Validator doesn't match. I didn't see the print statement if i submit the Add Form. My Question is: how can i assign a validator?