How to add field constraints to a Schema

15

1

In the documentation I read that it is possible to constrain field content of single-line text fields, number fields and date fields. But how exactly do I do that?

My (simplified) Schema looks like this:

<xsd:schema xmlns="uuid:2b" xmlns:tcmi="http://www.tridion.com/ContentManager/5.0/Instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="uuid:2b">
  <xsd:import namespace="http://www.tridion.com/ContentManager/5.0/Instance"></xsd:import>
  <xsd:annotation>
    <xsd:appinfo>
      <tcm:Labels xmlns:tcm="http://www.tridion.com/ContentManager/5.0">
        <tcm:Label ElementName="tag" Metadata="false">Tag</tcm:Label>
      </tcm:Labels>
    </xsd:appinfo>
  </xsd:annotation>
  <xsd:element name="Content">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="tag" minOccurs="1" maxOccurs="unbounded" type="xsd:normalizedString">
          <xsd:annotation>
            <xsd:appinfo>
              <tcm:ExtensionXml xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:ExtensionXml>
            </xsd:appinfo>
          </xsd:annotation>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

So how would I set a maximum length of 15 characters on the tag field?

John Bax

Posted 2013-02-20T12:07:31.007

Reputation: 225

Answers

17

Constraining field content describes constraining field content with the use of facets. Facets constrain the properties of a field type, so you will have to adjust the type definition of your Schema field.

Take your current field definition in your Schema

<xsd:element name="tag" minOccurs="1" maxOccurs="unbounded" type="xsd:normalizedString">
  <xsd:annotation>
    <xsd:appinfo>
      <tcm:ExtensionXml xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:ExtensionXml>
    </xsd:appinfo>
  </xsd:annotation>
</xsd:element>

Now change that to this (in the Schema source tab):

<xsd:element name="tag" minOccurs="1" maxOccurs="unbounded">
  <xsd:annotation>
    <xsd:appinfo>
      <tcm:ExtensionXml xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:ExtensionXml>
    </xsd:appinfo>
  </xsd:annotation>
  <xsd:simpleType>
    <xsd:restriction base="xsd:normalizedString">
      <xsd:maxLength value="15"/>
    </xsd:restriction> 
  </xsd:simpleType>
</xsd:element>

As you can see, you moved the type attribute into a child element xsd:simpleType, in which you added the restriction (or constraint).

Bart Koopman

Posted 2013-02-20T12:07:31.007

Reputation: 29 724

3

Perhaps worth noting that, of course, we're dealing with XML Schemas here, so the documentation at http://www.w3.org/TR/xmlschema-2/#normalizedString and nearby will be useful.

– Dominic Cronin – 2013-02-20T20:58:12.853

11

Some constraints - maxLength, or min|maxOccurs - are handled really well by the CME.

Other constraints may get a really hard to understand error message: XML Validation Failed Error message

You may want to test this before just rolling it out to your editors. Max length, if I'm not mistaken, will simply not allow you to type any more characters once the limit is reached. I did some research on ways to notify editors about field limitations, available here and here.

Nuno Linhares

Posted 2013-02-20T12:07:31.007

Reputation: 27 884