I'm using z3c.form to create a form in Plone 4.1.4. I need a boolean field which is required: the user must tick the box. (In my case, the user must agree to the terms and conditions.)
Using required=True
for the field doesn't work: I can submit the form without checking the checkbox.
This is what my code looks like:
from five import grok
from plone.directives import form
from zope import schema
from z3c.form import button
from Products.CMFCore.interfaces import ISiteRoot
from Products.statusmessages.interfaces import IStatusMessage
class ITestSchema(form.Schema):
hasApprovedConditions = schema.Bool(
title=u'I agree to the Terms and Conditions.',
required=True,
)
class TestForm(form.SchemaForm):
grok.name('test-form')
grok.require('zope2.View')
grok.context(ISiteRoot)
schema = ITestSchema
ignoreContext = True
@button.buttonAndHandler(u'Send')
def handleApply(self, action):
data, errors = self.extractData()
if errors:
self.status = self.formErrorsMessage
return
IStatusMessage(self.request).addStatusMessage(u'Thanks', 'info')
self.request.response.redirect(self.context.absolute_url())
The form shows the checkbox and label, but there is no indication that the field is required and indeed it isn't: I can submit the form without ticking the checkbox.
I'm extending these known good sets:
They pin z3c.form to version 2.5.1 but I also tried version 2.6.1.
What am I missing?
You should use a constraint like this:
def validateAccept(value):
if not value == True:
return False
return True
class ITestSchema(form.Schema):
hasApprovedConditions = schema.Bool(
title=u'I agree to the Terms and Conditions.',
required=True,
constraint=validateAccept,
)
More info:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With