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.

Add validations for email, phone number, and URL fields.

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:

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

  2. Begin editing the Distributor Application object and go to the Validations tab.

  3. Click Add ( Add Button ), enter these details, and click Save.

    FieldValue
    LabelEmail, applicantEmail
    TypeExpression Builder

    Enter a label, select a type, and click Save.

  4. Begin editing the newly created validation.

  5. In the Basic Info tab, set these values:

    FieldValue
    Active ValidationTrue
    Trigger EventOn Submission

    Activate the validation and select the On Submission trigger.

  6. Go to the Conditions tab and enter these values:

    FieldValue
    Expression BuilderisEmailAddress(applicantEmail) OR applicantEmail == ""
    Error MessagePlease enter a valid applicant email address.

    This requires the field’s value to follow a standard email pattern or be empty.

    tip

    You can use validations to make optional fields mandatory by not allowing users to leave the field empty.

    Use the isEmailAddress function with the applicantEmail field.

    If users attempt to submit an entry with an invalid value, Liferay displays the set error message.

    Enter an error message displayed to end users when they attempt to submit invalid values.

  7. Click Save.

    Click Save to activate the validation.

  8. Repeat the above steps to validate the primaryContactEmail field using the isEmailAddress() 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:

  1. In the Validations tab, click Add ( Add Button ), enter these details, and click Save.

    FieldValue
    LabelPhone Number, primaryContactPhoneNumber
    TypeExpression Builder
  2. Begin editing the newly created validation.

  3. In the Basic Info tab, set these values:

    FieldValue
    Active ValidationTrue
    Trigger EventOn Submission
  4. Go to the Conditions tab and enter these values:

    FieldValue
    Expression Buildermatch(primaryContactPhoneNumber, "^(\+\d{1,3} ?)?((\(\d{1,3}\))|\d{1,3})[- .]?\d{3,4}[- .]?\d{4}$") OR primaryContactPhoneNumber == ""
    Error MessagePlease 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.

  5. Click Save.

  6. 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:

  1. In the Validations tab, click Add ( Add Button ), enter these details, and click Save.

    FieldValue
    LabelURL, businessWebsite
    TypeExpression Builder
  2. Begin editing the newly created validation.

  3. In the Basic Info tab, set these values:

    FieldValue
    Active ValidationTrue
    Trigger EventOn Submission
  4. Go to the Conditions tab and enter these values:

    FieldValue
    Expression BuilderisURL(businessWebsite) OR businessWebsite == ""
    Error MessagePlease enter a valid website URL.
  5. Click Save.

When finished, you should have seven validations.

You should have seven validations.

Testing the Validations

Since they are active, you can begin testing your validations:

  1. Open Distributor Applications and begin editing an entry.

  2. Update an email field to an invalid value and click Save. The validation’s error message appears.

    Liferay displays the validation's error message.

  3. Enter a valid email for the field and click Save. It saves successfully.

  4. 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

Relevant Concepts

Capabilities

Product

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy