Adding a validation rule

Add a validation rule where one does not exist in the Management Center user interface. For example, you can add a rule to validate whether a business user entered a value for a mandatory field. In addition to creating a validation rule on the client-side, you also need to create the validation logic on the server-side.

Procedure

  1. Open WebSphere Commerce Developer and switch to the Enterprise Explorer view.
  2. Expand LOBTools > WebContent > WEB-INF > src > xml > commerce > component > objectDefinitions, where component is the name of the Management Center component.
  3. Open the object definition file that includes the object definition for the object property that your validation rule is to validate.
  4. Locate the object definition for the object property and define your validation rule within the object definition.
    You can define your validation rule with one of the following options:
    • Specify one of the following property definition attributes in the property definition:
      • maximumSize
      • maxValue
      • minValue
      • required
      • type
      The following code snippet shows an example of a validation rule that causes an error to occur when the identifier property is blank or exceeds 254 characters.
      
      <PropertyDefinition
         displayName="${catalogResources.categoryCode_DisplayName}" 
         propertyName="identifier" type="string" required="true" maximumSize="254"/> 
      </PropertyDefinition>
    • Define a definition element that extends the validator definition as a child element of a PropertyDefinition element. The following code snippet shows how to define a custom validator that verifies the attribute value type.
      
      <PropertyDefinition displayName="${catalogResources.attributeValue}" propertyName="value" required="true">
        ...
        <AttributeDictionaryValueTypeValidator package="cmc/catalog"/>
        ...
      </PropertyDefinition>
      
    • Define a definition element that extends the Validator class definition as a child element of an object definition. This type of validator is called an object level validator. Object level validators are used to validate multiple property values or child objects. The following code snippet shows how to define an object level validator.
      
      <Definitions>
      <ReferenceObjectDefinition copyProtected="false" definitionName="cmc/catalog/BaseAttachmentReference" 
       displayName="${catalogResources.catalogAttachmentReference_DisplayName}" idProperty="attachmentRefId" 
       isBaseDefinition="true">
      ...
        <!--- This is a validator definition to validate that the minimum quantity values for offer prices are unique. -->
        <UniqueValueForChildObjectPropertyValidator 
        definitionName="cmc/catalog/OfferPriceMinimumQuantityUniquenessValidator" 
        errorMessage="${catalogResources.offerPriceMinimumQuantityUniquenessWarning}" objectPath="CatalogEntryOffer" 
        propertyName="minimumQuantity" validatorId="offerPriceMinimumQuantityUniquenessValidator">
          <dependency localName="catalogResources" moduleName="cmc/catalog/CatalogResources"/>
        </UniqueValueForChildObjectPropertyValidator>
      ...
      </Definitions>
      This validator definition also defines the error message for the validation error. The value for the errorMessage attribute maps to a properties file entry that defines the error message text that displays when a validation error occurs. If an error message is defined, the validator definition must also include the resource bundle dependency for the resource bundle that includes the message text.
  5. Save and publish your changes.