Dexterity: differentiate validator between create and edit

I've run a few times now into issues with Validators on Dexterity Content and I'm curious which checks/workaround others use.

A validator is both run on create and on edit, the problem is that self.context upon creation is still the parent of the 'newly to be created item'. But on edit it's the item itself which is self.context.

How can I run different validations based on edit or create? Is there some way I can maybe find out if I'm the edit or the create dexterity z3c.form from self.context/self.request? Other methods?

1 Like

To anser my own question for future reference. :stuck_out_tongue:

from z3c.form.interfaces import IAddForm, IEditForm
from z3c.form import validator

class SectionStatusValidator(validator.SimpleFieldValidator):
    def validate(self, value):
        if IAddForm.providedBy(self.view):
            parent = self.context
            addForm = True
        elif IEditForm.providedBy(self.view):
            parent = aq_parent(self.context)
            addForm = False
        else:
            return
5 Likes

In a multilingual site, you also might want to consider

plone.app.multilingual.dx.interfaces.IMultilingualAddForm

This interface is provided when you translate an existing content object.