Let's say we have a dexterity content type with two fields: field_a and field_b.
In edit mode:
- if
field_ahas thecategory_1value selected thenfield_bis not required - if
field_bhas other value selected thenfield_bis required
It seems a simple validation is not possible here. I tried:
from zope.interface import Invalid
def validate_field_b(value):
# value is a string here. I can't create a condition based on field_a.
raise Invalid(_(u"Value in field b is required."))
used here:
field_b = schema.TextLine(
title=_(u"Some field B"),
required=False,
constraint=validate_field_b
)
The result is field_b required all the time. :)
Any solution here? How the get the form / context / request / other fields value in my validator?
The solution is to use an
invariantinstead ofconstraint: https://docs.plone.org/external/plone.app.dexterity/docs/advanced/validators.html#invariants