So far we've created enviable content components and have a pretty good view of them. Now let's focus on the details; anything at the moment can be written to the message domain, including malicious HTML and JavaScript code. It is therefore useful to develop a special field (and the corresponding widget) that excludes HTML tags that are not allowed.
Creating common fields and parts is a fairly common task for end user programs, and since these systems often have very specific requirements, custom Schema/form will help us reach our design goals and easily create the interface we want through customization.
Step I: Create a domain
The purpose of a particular domain is to validate the input of the domain based on allow or disallow HTML markup. If the message text exceeds the allowed HTML tag or contains a disabled HTML tag, the value acknowledgement should fail. Note Only one of the two properties can be specified. In ZOPE3, since the introduction of a laudable collection, we do not need to write a domain from scratch, as they are universally serviced by common domains as basic classes. And it has already provided most of the functionality to us, so our HTML domain text fields also use them as our domain base.
We will expand the text field through the two new properties of Allowed_tags and Forbidden_tags. We will then modify the _validate () method to implement the constraint.
(a) interface
As usual, the first step is to define the interface. In the interface module of Messageboard ' s, add the following lines:
#!python
From Zope.schema import Tuple
From zope.schema.interfaces import IText
Class ihtml (IText):
"" "A text field that handles HTML input." "
Allowed_tags = Tuple (
Title=u "Even HTML Tags",
Description=u "" "\
Only listed tags can is used in the http://www.aliyun.com/zixun/aggregation/9541.html ">value of the field."
""",
Required=false)
Forbidden_tags = Tuple (
Title=u "Forbidden HTML Tags",
Description=u "" "\
listed tags cant be used in the value of the field.
""",
Required=false)
Line 1th: The tuple field requires a value that is a python tuple.
第2-4: We have simply expanded the Itext interface and schema.
第7-12 rows &14-19 Rows: Defining two additional properties with a field tuple