Customizing Datatypes

Applying Validation Rules to Fields

A validation rule is a criterion to ensure that input data, for example, entered by a user in a form field is correct, that is, meets this criterion.

You can apply one or more validation rules to a data type field to control the user’s input.

For example, you want to ensure that the user:

  • Entered an email address in a corresponding field
  • Entered a well-formed email address

In this case, you can apply two validation rules to the field:

  • Checking that the field has a value (i.e. “is not null”)
  • Validating the string as a well-formed email address with a regular expression

C1 CMS comes with a predefined set of validation rules that you can apply to data type fields, too.

All the validation rules are available as functions in Composite.Utils.Validation.

Each type of field has its own set of validation rules.

Adding Validation Rule to Field

To add a validation rule to a field:

  1. Edit a data type, and on the Fields tab, select a field to add a validation rule to.
  2. On the Advanced tab, click Add Validation Rules. The Field Validation Rules Configuration window appears.

    Figure 15: Field Validation Rules Configuration window

    Note: If at least one validation rule has already been added to the field, the button will read “Edit Validation Rules”.

  3. Click Add New. The Select Function window appears.

    Figure 16: Select Function window

    Note: If no validation rules have been previously added to the field, this window will pop up automatically after Step 3.

  4. Expand All Functions > Composite > Utils > Validation, select the validation rule you want to use on the field and click OK.
  5. If the validation rule requires that its parameters should be configured, enter the proper values.

    Figure 17: Setting parameters of a validation rule

     
  6. Click OK to save changes and close the Field Validation Rules Configuration window.

You have just added a validation rule to the field.

Editing Validation Rule

Note: You can only edit a validation rule that has parameters.

To edit a validation rule:

  1. Edit a data type and on the Fields tab, select a field for which you want to edit a validation rule.
  2. On the Advanced tab, click Edit Validation Rules. The Field Validation Rules Configuration window appears.
  3. Select the validation rule you want to edit and modify its parameters where necessary.
  4. Click OK to save changes and close the Field Validation Rules Configuration window.

You have just modified a validation rule for the field.

Deleting Validation Rule

To delete a validation rule:

  1. Edit a data type and on the Fields tab, select a field from which you want to delete a validation rule.
  2. On the Advanced tab, click Edit Validation Rules. The Field Validation Rules Configuration window appears.
  3. Select the validation rule you want to delete and click Delete.
  4. Click OK to save changes and close the Field Validation Rules Configuration window.

You have just deleted a validation rule from the field.

Overview of Validation Rules

Except the Boolean and Data Reference types, each field type has its own set of validation rules.

The Boolean type has no validation rules. The Data Reference type reuses the String’s validation rules.

Most data types has one common validation rule – the one that ensures that the value is not null.

DateTime

DateTimeNotNullValidation

This rule ensures that the input value has been entered and is therefore not null.

Decimal

DecimalNotNullValidation

This rule ensures that the input value has been entered and is therefore not null.

DecimalPrecisionValidation

This rule validates the precision of digits, that is, the number of decimals the user has specified). It has one required parameter:

MaxDigits

An Int32 value that specifies the maximum number of digits allowed on the decimal.

GUID

GuidNotNullValidation

This rule ensures that the input value has been entered and is therefore not null.

Int32

Int32NotNullValidation

This rule ensures that the input value has been entered and is therefore not null.

IntegerRangeValidation

This rule validates that an integer lies within a range of values specified in its two required parameters:

min

An Int32 value that specifies the minimum number allowed in this field.

max

An Int32 value that specifies the maximum number allowed in this field.

String and Data References

StringNotNullValidation

This rule ensures that the input value has been entered and is therefore not null.

RegularExpressionValidation

This rule validates that a string conforms to the regular expression specified in its required parameter:

Pattern

A string value that holds the regular expression pattern to use.

StringLengthValidation

This rule validates that the length of a string lies within a range specified in its two required parameters:

min

An Int32 value that specifies the minimum number of characters allowed in this field.

max

An Int32 value that specifies the maximum number of characters allowed in this field.

Example of Using Validation Rules

Let’s assume that you have a simple registration form with two fields: Name and Email Address.

You want the name the user should enter in the corresponding field to be no longer than 8 characters. You also want the email address to be well-formed.

You have a global data type called “Users” with the respective two string fields. Let’s see how you should meet the requirements by applying validation rules to both fields.

First, you have to limit the number of characters in the Name field to 8:

  1. Edit a data type and on the Fields tab select the Name field.
  2. On the Advanced tab, click Add Validation Rules.
  3. In the Select Function window expand All Functions > Composite > Utils > Validation.
  4. Select StringLengthValidation and click OK.
  5. In the Field Validation Rules Configuration window, set the Minimum length parameter to 1 and the Maximum length parameter to 8 and click OK.

Note: If the Select Function window has not popped up over the Field Validation Rules Configuration window in Step 3, you should click Add New.

Next, you have to ensure that an email address entered is well-formed.

  1. Select the Email Address field and on the Advanced tab, click Add Validation Rules.
  2. In the Select Function window expand All Functions > Composite > Utils > Validation.
  3. Select RegularExpressionValidation and click OK.
  4. In the Field Validation Rules Configuration window, set the RegEx pattern parameter to “^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$” (without quotation marks) and click OK.
  5. Save the data type.

Now try to enter values in the Registration form by using the values that both meet and do not meet the requirements and see how the validation works.

Making Validation Messages on Forms User-Friendly

Note: The following information is applicable to the web forms generated by Forms Renderer.

The field validation on a form may fail because the value in the field does not meet requirements. You specify the requirements for the value when you either set properties of the field (for example, Field type or Required) or apply validation rules.

In both cases, if the validation fails, a validation error message appears on the form. By default, the Forms Renderer uses built-in system messages, which may be quite technical and not always appropriate on the form.

You can make the message user-friendly by replacing the system message with your own message. In this case, if the field validation fails, the user-friendly message will be displayed on the form instead.

When you specify the Help text for a field of a data type, the Forms Renderer uses this text for a validation message on a form.

To make a validation message for a field user-friendly:

  1. Edit the data type used by the Forms Renderer.
  2. Open the Fields tab and select a field under the Datatype fields.
  3. Type some user-friendly text in the Help property.
  4. Repeat Steps 2-3 for each field.
  5. Save the data type.