I'm using a form where I want to have the required field missing error on many fields depending on the value of one other field.
The use case: the use have to choose between two shipping mode (postale or print). if postale is choosen but fields about address has not been filled I want to raise the error.
Here is some code
class ILivraisonForm(interface.Interface):
livraison = schema.Choice(title=_(u"Mode de livraison"),
vocabulary=vocabulary.livraison)
livraison_civility = schema.Choice(title=_(u"Civility (livraison)"),
vocabulary=userdata.gender_vocabulary,
required=False)
livraison_name = schema.TextLine(title=_(u"Name (livraison)"),
required=False)
livraison_firstname = schema.TextLine(title=_(u"Firstname (livraison)"),
required=False)
livraison_add1 = schema.TextLine(title=_(u"Address line 1 (livraison)"),
required=False)
livraison_add2 = schema.TextLine(title=_(u"Address line 2 (livraison)"),
required=False)
livraison_pc = schema.TextLine(title=_(u"Postal code (livraison)"),
required=False)
livraison_city = schema.TextLine(title=_(u"City (livraison)"),
required=False)
livraison_phone = schema.TextLine(title=_(u"Phone"),
required=False)
accepted = schema.Bool(title=_(u"Accept CGV"),
description=_(u"Click here to read the CGV"),
required=True)
class LivraisonForm(form.Form):
fields = field.Fields(ILivraisonForm)
@button.buttonAndHandler(_(u"Valider"))
def handleApply(self, action):
data, errors = self.extractData()
message = IStatusMessage(self.request)
if errors:
self.status = _(u"Please fix errors")
return
if not data['accepted']:
self.status = _(u'You must accept')
raise WidgetActionExecutionError('accepted',
interface.Invalid(_(u'You must accept')))
if data['livraison'] == 'mail' \
and ( not data['livraison_name'] or not data['livraison_firstname'] \
or not data['livraison_add1'] or not data['livraison_pc'] \
or not data['livraison_city']):
self.status = _(u'You must add your postal address')
raise ???
You don't need to raise anything; just add a return
instead of a raise
right where you are.
In addition, you can set error messages on individual widgets in an action handler, provided you create a zope.schema.ValidationError
subclass:
from zope.schema import ValidationError
class AddressMissing(ValidationError):
__doc__ = _(u'error_noaddress', u'Please provide an address')
and then in the handler:
from z3c.form.interfaces import IErrorViewSnippet
from zope import component
def handleApply(self, action):
# ....
widget = self.widgets['livraison_add1']
error = component.getMultiAdapter(
(AddressMissing(), self.request, widget, widget.field,
self, self.context), interfaces.IErrorViewSnippet)
error.update()
widget.error = error
You can do this for all affected widgets.
Alternatively, you can have this handled using an invariant:
from zope.interface import invariant, Invalid
class ILivraisonForm(interface.Interface):
# ....
@invariant
def validatePostalAddress(data):
if data.livraison. != 'mail':
return
for field in ('name', 'firstname', 'add1', 'pc', 'city'):
if not data['livraison_' + field]:
raise Invalid(_(u'error_noaddress', u'Please provide an address'))
and the error will be set when you call self.extractData()
.