Thursday, July 30, 2009

Constraints in Alfresco

Constraints enable validating user input.  In a simplest term these are validation rules that can be defined and applied to aspects and content types.  Constraints are defined separately from aspects and content types, enabling reuse and  better manageability. You can refer to constraints from within the property type definition of an aspect or content type.

Alfresco version 3.2 Community

Out of the box there are four types of constraints available.  Of course as anything else in Alfresco, you can create your own custom constraint types as needed.  Constraint types available out of the box include: REGEX, LIST, MINMAX, and LENGTH.  These are self explanatory so won’t go in details explaining these.

Below are a couple of examples of how to create custom constraints using out of the box constraints types.  In the following, we will also see an example of how to use these constraints in aspects.

  • Open customModel.xml file for editing (location /tomcat/shared/classes/alfresco/extensions)
  • Between </namespaces> and <types> add following custom constraints

<constraints>
  <constraint name="custom:product_list" type="LIST">
    <parameter name="allowedValues">
      <list>
        <value>Laptops</value>
        <value>Harddrives</value>
        <value>Memory</value>
      </list>
    </parameter>
  </constraint>
  <constraint name="custom:name_length" type="LENGTH">
    <parameter name="minLength">
      <value>5</value>
    </parameter>
    <parameter name="maxLength">
      <value>25</value>
    </parameter>
  </constraint>
</constraints>

  • Above we created two custom constraints using LIST and LENGTH constraint types
  • Now to use these constraints simply add  constraints in the property type definition of an aspect or content type.  Below we will add constraint to an aspect

<aspects>
  <aspect name="custom:ProductDetails">
    <title>Product Details</title>
    <properties>
      <property name="custom:ProductName">
        <title>Product Name</title>
        <type>d:text</type>
        <protected>false</protected>
        <mandatory>true</mandatory>
        <multiple>false</multiple>
        <constraints>
          <constraint ref="custom:name_length"/>
        </constraints>

      </property>

No comments:

Post a Comment