Documentation

Example: Using Hidden Fields with Form Rules

Configure Forms fields as hidden in the Advanced configuration tab.

Hide fields using the advanced setting.

There’s a hidden field in this form. It holds the Site’s ID number, so that it can be passed to a data provider as an input value:

The form includes a hidden field that's used to pass information to a data provider.

To follow the example you must first enable local network access in the Data Provider System Settings entry. See Enabling Access to Data on the Local Network for more information.

This example requires creating two data providers, and a form with four fields and three form rules. The table below contains a summary of the elements:

Liferay Forms Element

Element Label

Description

Data Provider

Get Group Users

Get the Site’s/Group’s Users and provide a list of their email addresses.

Data Provider

Get User by Email

Get the User by the selected email address and provide the first and last name.

Form Field

Hidden Group ID

Use a hidden Numeric field to hold the Group ID of the current site.

Form Field

Who are you?

Use a Select from List field that displays the email addresses returned by the data provider.

Form Field

First Name

Use a Text field for the User’s first name.

Form Field

Last Name

Use a Text field for the User’s last name.

Form Rule

Not Applicable

If the Hidden Group ID field is not empty, autofill the Select from List field with the email addresses of the Site’s Users.

Form Rule

Not Applicable

If the Who are you? field is not empty, autofill the first and last name fields retrieved from the data provider.

Form Rule

Not Applicable

If the Who are you? field is empty, enable the name fields so the User can manually enter data. If the condition isn’t met and the first and last name fields are autofilled form the data provider, these fields are disabled on the form.

To configure this example,

  1. Configure a data provider with these settings:

    • Name: Get Group Users

    • URL: http://localhost:8080/api/jsonws/user/get-group-users/group-id/{groupId}

      The groupId is passed as an input parameter to the data provider. In this example a hidden form field will be used to provide the group ID.

    • User Name: test@liferay.com (or your administrator’s email address)

    • Password: [Enter the user’s password]

    • INPUTS:

      • Label: Group ID

      • Parameter: groupId

      • Type: Number

    • OUTPUTS:

      • Label: Email Address

      • Path: $..emailAddress

      • Type: List

    Save the data provider.

  2. Configure another data provider with these settings:

    • Name: Get User by Email

    • URL: http://localhost:8080/api/jsonws/user/get-user-by-email-address/company-id/20099/email-address/{emailAddress}

      You must retrieve your Liferay Instance’s ID (e.g., 20099). It’s the Instance ID displayed in Control Panel → Virtual Instances.

    • User Name: test@liferay.com (or your administrator’s email address)

    • Password: [Enter the user’s password]

    • INPUTS:

      • Label: Email Address

      • Parameter: emailAddress

      • Type: Text

    • OUTPUTS:

      • Label: First Name

      • Path: $.firstName

      • Type: Text

    Add another output by clicking the plus symbol.

    • OUTPUTS:

      • Label: Last Name

      • Path: $.lastName

      • Type: Text

    Save the data provider.

  3. Create a new form and add these fields:

    • Numeric field: Hidden Group ID

      • Predefined Value: [Site ID, e.g. 20123]

        Find the Site ID in Site Settings → Site Configuration

    • Select from List field: Who are You?

      • Create List: From Autofill

    • Text field: First Name

    • Text field: Last Name

    Give the form a title and save it.

    The form has four fields.

  4. Go to the Rules tab in the form and create a rule to autofill the Who are You? field’s list of options:

    • CONDITION:

      • If Hidden Group ID Is Not Empty

    • ACTION:

      • Do Autofill From Data Provider Get Group Users

        • Data Provider’s Input: groupIdHidden Group ID

        • Data Provider’s Output: Email AddressWho are You?

    Save the rule.

    Autofill the field with the Site Users' email addresses.

  5. Go to the Rules tab in the form and create a rule to autofill the Who are You? field’s list of options:

    • CONDITION:

      • If Who are You? Is Not Empty

    • ACTION:

      • Do Autofill From Data Provider Get User by Email

        • Data Provider’s Input: emailAddressWho are You?

        • Data Provider’s Output: First NameFirst Name

        • Data Provider’s Output: Last NameLast Name

    Save the rule.

    Autofill the name fields from the selected email address.

  6. Go to the Rules tab in the form and create a rule to only enable the name fields if an email address hasn’t been selected:

    • CONDITION:

      • If Who are You? Is Empty

    • ACTION:

      • Do Enable First Name

      • Do Enable Last Name

    Save the rule.

    If an email isn't selected, the name fields are enabled for user input.

  7. Save and publish the form. Saving the rules did not save the form so you must go back to the Form tab and click Save, and Publish if the form is not already published.

To test the form, go to its dedicated URL and select an email address. Note the following behavior:

  • Use the Who are you? field to choose your email address from a list of Users that belong to the current Site. This list is populated by a data provider calling the JSON web service get-group-users.

  • Choosing an email address from the selector autofills the First Name and Last Name fields, and disables these fields so they cannot be edited.

  • Leaving the Who are you? field blank (e.g., Choose an Option is displayed) enables both name fields so the user can input data directly.