I am using Plone 4.3 and I have a form.SchemaForm plone.directives have an interface that has a start field from IEventBasic and a validator:
from datetime import timedelta
from plone.directives import form
from plone.app.contenttypes.interfaces import IEvent
from z3c.form import validator
from zope.component import provideAdapter
from zope.interface import Invalid
class IMyObject(form.SchemaForm)
my_field_a = schema.TextLine(title='a_field')
...
class MyObject(Item):
implements(IMyObject, IEvent)
class EndMyObjectValidator(validator.SimpleFieldValidator):
def validate(self,value):
#code for checking if end field is within a certain range from start field
if self.end > self.start + timedelta(days=6):
raise Invalid('The end date is not within range of the start date's week')
validator.WidgetValueDiscriminators(EndMyObjectValidator, field=IEventBasic['end'])
provideAdapter(EndMyObjectValidator)
In my type file (my.object.myobject.xml under profiles/default/types), I place the behavior in the behaviors section.
<behaviors>
<element value="plone.app.event.dx.behaviors.IEventBasic"/>
</behaviors>
The problem is it validates the end field in any Event object or any object that implements the IEventBasic interface/schema.
I thought maybe since the Plone documentation says that the parameters 'view' and 'context' of WidgetValueDiscriminators accept an interface, then I could do either:
validator.WidgetValidatorDiscriminators(EndMyObjectValidator, view=IMyObject, field=IEventBasic['end'])
or
validator.WidgetValidatorDiscriminators(EndMyObjectValidator, context=IMyObject,field=IEventBasic['end']
Unfortunately, none of those trigger at all. I guess I'm misunderstanding what the context and view parameters actually do. How can I make it so the validators are specifically for dealing with MyObject?
Source: http://docs.plone.org/develop/addons/schema-driven-forms/customising-form-behaviour/validation.html
For now I am doing:
...
from gpcl.container.my_container import MyContainer
...
class EndMyObjectValidator(validator.SimpleFieldValidator):
def validate(self,value):
if self.widgets.form.portal_type <> 'my.object.myobject':
return
...
validator.WidgetValueDiscriminators(EndMyObjectValidator, field=IEventBasic['end'])
provideAdapter(EndMyObjectValidator)
Update 2: I removed my comment before because it was for an unrelated problem. I changed the way I was checking for the type.
Ok, register your own Add Form and Subclassing the Default Dexterity Add Form. More Information about Validating in action handlers and Custom Add Forms
in my
task.py(Contenttype and so on...):in my
configure.zcmli register the custom Add formin my task.xml Definition: