Documentation

Adding Custom Validations

Available Liferay 7.4 U27+/GA27+

With Objects, you can add validations for both custom and system fields. Validations set rules used for determining valid field entries and are defined using either Groovy scripts or Liferay’s expression builder. Each validation has its own trigger, conditions, and error text, which you can set via the Objects UI. When triggered, the validation checks for valid field entries according to your defined conditions and displays your error text for invalid entries.

Create validations for custom and system Object fields.

Important

Currently, Groovy script validations are only available for Liferay Experience Cloud Self-Managed and Liferay DXP Self-Hosted.

Follow these steps to add a custom validation:

  1. Open the Global Menu (Global Menu), click the Control Panel tab, and go to Objects.

  2. Begin editing an Object definition.

    Note

    Beginning with Liferay 7.4 U38+/GA38+, you can add custom validations to system Objects.

  3. Go to the Validations tab and click the Add button (Add Button).

  4. Enter a label and select a validation type: Groovy or Expression Builder.

    Enter a label and select a validation type.

  5. Click Save.

  6. Begin editing the newly created validation.

  7. Go to the Conditions tab and add conditions to the validation.

    Add conditions to the validation.

    When using Groovy, you can browse and add available data fields to your conditions via the side panel. See Using Groovy Validations for more information.

    When using Expression Builder, you can browse and add fields, operators, and functions to your conditions via the side panel. See Using Expression Builder Validations for more information.

    Tip

    Conditions can include multiple fields and functions for complex validations.

  8. Enter a localizable error message. This message is displayed whenever the validation is triggered and field entries do not meet one or more of the defined conditions.

  9. Go to the Basic Info tab and activate the validation.

  10. Select a Trigger Event to determine when the validation is run for field entries.

    Note

    Each validation can only have one trigger event.

    Activate the validation and set a trigger event.

  11. Click Save.

Once activated, the validation is run for all new Object entries.

Using Groovy Validations

The Groovy validation type supports all standard Groovy Script capabilities. However, when defining Groovy conditions, you must use the invalidFields variable. Liferay only displays the validation error message when invalidFields returns true.

Use the side panel to add field elements to your Groovy validations.

For Liferay 7.4 U33+ and GA33+, Liferay uses the GroovyShell class to check your Groovy scripts for valid syntax when you click Save. If the script is invalid, Liferay shows an error message.

Using Expression Builder Validations

The Expression Builder type provides predefined fields, operators, and functions that you can access in the Elements side panel. Clicking an element adds it to the conditions editor. These functions return a Boolean value. See Expression Builder Validations Reference for a complete list of provided operators and functions.

Important

Expression Builder validations can only be used with text, numeric, date, and boolean field types.

Use the side panel to add field, operator, and function elements to your validation.

For Liferay 7.4 U33+ and GA33+, Liferay checks your expression for valid syntax when you click Save. If the expression is invalid, Liferay shows an error message.

Expression Builder Operators

The following table lists available operators for Expression Builder validations:

Operator

Description

And ( AND )

Coordinating conjunction used to indicate a dependent relationship

Divided By ( / )

Mathematical operator for division

Minus ( - )

Mathematical operator for subtraction

Or ( OR )

Coordinating conjunction used to indicate an independent relationship

Plus ( + )

Mathematical operator for addition

Multiply ( * )

Mathematical operator for multiplication

Expression Builder Functions

The following table lists available Expression Builder functions with their compatible field types.

Operator

Text Fields

Numeric Fields

Date Fields

Compare Dates

Concat

Condition

Contains

Does Not Contain

Future Dates

Is a URL

Is an Email

Is Decimal

Is Empty

Is Equal To

Is Greater Than

Is Greater Than or Equal To

Is Integer

Is Less Than

Is Less Than or Equal To

Is Not Equal To

Match

Past Dates

Range

Sum

Available Fields Reference

For 7.4 U41+/GA41+

When constructing conditions, you can use any of the Object’s custom or system fields. You can also select from relationship fields on the ‘one’ side of a relationship.

The following table lists all default fields available for custom Objects:

Field

Description

companyId

Portal instance where the entry was created

createDate

When the entry was created

externalReferenceCode

External reference code for the entry

groupId

Site ID in where the entry was created

lastPublishDate

Date when the entry was last published

modifiedDate

Date when the entry was last modified

mvccVersion

MVCC version of the entry

objectDefinitionId

ID of the entry’s Object

objectEntryId

ID for the entry

status

Workflow status for the entry

statusByUserId

ID of the assigned user in Workflow

statusByUserName

Name of the assigned user in Workflow

statusDate

Date when the Workflow status was last updated

userId

ID of the entry’s author

userName

User name of the entry’s author

uuid

Unique universal ID for the entry

System Objects have their own default fields, though there is some overlap with the above chart.

Additional Information