Example: Using Hidden Fields with Form Rules¶
Configure Forms fields as hidden in the Advanced configuration tab.
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:
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,
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.
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.
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.
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 UsersData Provider’s Input:
groupId
→ Hidden Group IDData Provider’s Output:
Email Address
→ Who are You?
Save the rule.
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 EmailData Provider’s Input:
emailAddress
→ Who are You?Data Provider’s Output:
First Name
→ First NameData Provider’s Output:
Last Name
→ Last Name
Save the rule.
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 NameDo
Enable
Last Name
Save the rule.
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.
Related Content¶
Not finding what you're looking for?
Pardon our dust as we revamp and transition our product documentation to this site. If something seems missing, please check Liferay Help Center documentation for Liferay DXP 7.2 and previous versions.
Try Liferay's Help Center