This article contains a brief example for how to effectively manage validation in the web forms portlet.
Resolution
Note that following the below reproduction steps (or something similar) will produce an error:
- Deploy the Web Form portlet to a page
- Click on the wrench and select Configuration
- Create a new field
- Title the field Yes or No and change the type to radio buttons
- In the Options field enter Yes,No
- Click on Validation and in the Validation Script field enter return false;
- In the Validation Error Message enter False!
- Click Save
- Enter data on the form
- Click Submit
In the above scenario, the form is submitted without an error message that says “False!.”
By default, validation is turned-off. To turn on validation, set validation.script.enabled=true in the portlet-ext.properties file, located in your Liferay Home directory
Example
This validation can be used in a variety of ways. Feel free to use the following scripts to verify phone numbers or email addresses.
Add a text field, and under the validation field enter the following script which will verify a nine digit number.
var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if (regexObj.test(currentFieldValue)) {
return true;
} else {
return false;
}
In order to verify that an email address has an @ and a period use the following script:
var isEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(currentFieldValue.search (isEmail) == -1 && currentFieldValue != '') {
return false;
} else {
return true;
}
Additional Information
Note that any code changes to the Web Form portlet for a specific environment are made at the developer's own discretion and are not supported by Liferay Subscription Services.
Creating Custom Validation Scripts on Web Form Portlet With DXP 7.0