Validating Fields
So far, Delectable Bonsai has no way to ensure applicants provide valid, consistent data in their applications. Here you’ll add validations for Distributor Application’s email, phone number, and URL fields to improve data integrity and consistency.
Adding Validations for Email Fields
Liferay provides the isEmailAddress()
function for checking whether a field’s value uses standard email elements (e.g., @gmail, .com).
Follow these steps to validate the applicantEmail
and primaryContactEmail
fields:
-
Open the Global Menu ( ), go to the Control Panel tab, and click Objects.
-
Begin editing the Distributor Application object and go to the Validations tab.
-
Click Add ( ), enter these details, and click Save.
Field Value Label Email, applicantEmail Type Expression Builder -
Begin editing the newly created validation.
-
In the Basic Info tab, set these values:
Field Value Active Validation True Trigger Event On Submission -
Go to the Conditions tab and enter these values:
Field Value Expression Builder isEmailAddress(applicantEmail) OR applicantEmail == ""
Error Message Please enter a valid applicant email address. This requires the field’s value to follow a standard email pattern or be empty.
tipYou can use validations to make optional fields mandatory by not allowing users to leave the field empty.
If users attempt to submit an entry with an invalid value, Liferay displays the set error message.
-
Click Save.
-
Repeat the above steps to validate the
primaryContactEmail
field using theisEmailAddress()
function.
Adding Validation for Phone Number Fields
Liferay provides the match()
function for checking whether the field’s value matches a specified RegEx pattern or string value. In this scenario, Delectable Bonsai wants to use this expression to validate their phone number fields: ^(\+\d{1,3} ?)?((\(\d{1,3}\))|\d{1,3})[- .]?\d{3,4}[- .]?\d{4}$
.
Follow these steps to validate the primaryContactPhoneNumber
, businessPhoneNumber
, bankPhoneNumber
, referencePhoneNumber
fields:
-
In the Validations tab, click Add ( ), enter these details, and click Save.
Field Value Label Phone Number, primaryContactPhoneNumber Type Expression Builder -
Begin editing the newly created validation.
-
In the Basic Info tab, set these values:
Field Value Active Validation True Trigger Event On Submission -
Go to the Conditions tab and enter these values:
Field Value Expression Builder match(primaryContactPhoneNumber, "^(\+\d{1,3} ?)?((\(\d{1,3}\))|\d{1,3})[- .]?\d{3,4}[- .]?\d{4}$") OR primaryContactPhoneNumber == ""
Error Message Please enter a valid phone number for the primary contact. This validation supports a wide range of international phone numbers, allowing for international country codes, area codes, and separators. It also allows for format variations (e.g., spaces, dashes, dots), but enforces a minimum of 7 digits and a maximum of 11 digits. Since the field is optional, this validation also allows for empty values.
-
Click Save.
-
Repeat these steps for the
businessPhoneNumber
,bankPhoneNumber
,referencePhoneNumber
fields.
Adding Validation for Business Website Field
Liferay provides the isURL()
function for checking whether the field’s value uses standard URL elements (e.g., http
, ://
, .com
).
Follow these steps to validate the businessWebsite
field:
-
In the Validations tab, click Add ( ), enter these details, and click Save.
Field Value Label URL, businessWebsite Type Expression Builder -
Begin editing the newly created validation.
-
In the Basic Info tab, set these values:
Field Value Active Validation True Trigger Event On Submission -
Go to the Conditions tab and enter these values:
Field Value Expression Builder isURL(businessWebsite) OR businessWebsite == ""
Error Message Please enter a valid website URL. -
Click Save.
When finished, you should have seven validations.
Testing the Validations
Since they are active, you can begin testing your validations:
-
Open Distributor Applications and begin editing an entry.
-
Update an email field to an invalid value and click Save. The validation’s error message appears.
-
Enter a valid email for the field and click Save. It saves successfully.
-
Repeat this process to test the phone number and website validations.
Great! Distributors submitting applications must enter valid field values.
Now Delectable Bonsai wants to ensure only the intended users have access to data for the Distributor Application and Application Evaluation objects.
Next: Managing Access to Data