This is the code of the constraint I'm using:
def validCodeConstraint(value):
"""Validate code inserted into control panel configlet fields.
:param value: code to be validated
:type value: unicode
:return: True if code is valid
:rtype: bool
:raises:
:class:`~zope.interface.Invalid` if the code is not valid
"""
if value:
parser = etree.HTMLParser(recover=False)
try:
etree.HTML(value, parser)
except Exception as e:
raise Invalid(_(u'Invalid code: ') + e.message)
return True
class IBluelinePortlet(IPortletDataProvider):
'''Blueline Portlet.'''
embed = schema.Text(
title=_(u'Embedding code'),
required=False,
constraint=validCodeConstraint,
)