Overview
Application Scenario
When formula validation does not apply to some special business logic, you hope that the system can obtain the data on the data entry page, hand the data to a custom program for judgment, return the validation result, and display the result on the data entry page.
Function Entry
Choose Template> Data Entry Attribute on the designer menu bar, click Validate Data, click +, and select Custom Validation, as shown in the following figure.

Function Description
(1) The essence of custom validation is to obtain the data on the page, process the data based on your custom code, and finally return the validation result. Therefore, you need to write the code part for processing the data according to your business needs.
(2) The validation result can be returned only for display on the page, but not for other callback requirements.
(3) Custom validation is available on mobile terminals.
Example
Data Preparation
Create a dataset ds1 with the SQL statement SELECT * FROM SProduct limit 3 to extract data from FRDemo.

Designing the Data Entry Table
Design the data entry table: Drag the fields from the dataset to corresponding cells and add a text widget to cell C2, as shown in the following figure.

Setting the Custom Validation Class
The effect to be achieved in this example is: When the value of Price is less than the set minimum value, a validation reminder will pop up.
Refer to the following code for the system to obtain the corresponding template data, make judgments, and return the validation result.
package com.fr.data;
import com.fr.base.Utils;
import com.fr.log.FineLoggerFactory;
import com.fr.script.Calculator;
public class VerifyDemo extends DefinedVerifyJob {
/**
* This private variable must be defined to indicate the validation status. The variable name can be changed.
* 0 (default value) indicates successful validation.
* 1 indicates validation failure.
*/
private int type = 0;
/**
* When the attribute names in the custom event added for the template are the same as the variable names below, the values of the attributes will be automatically assigned to the corresponding variables.
* JobValue indicates that the bound value comes from a cell. For other types of data, their respective classes need to be defined accordingly. For example, the class of a string is String.
* Cell groups are not supported.
*/
private JobValue Price;
private int MinNum;
public void doJob(Calculator calculator) throws Exception {
FineLoggerFactory.getLogger().error("------Price:" + Price.getValue() + ",MinNum:" + MinNum + "------");
// Output the obtained parameter values to fanruan.log at the error level.
/**
* Get the value of Price, convert it to an integer, and assign it to the price object.
*/
if (Price != null) {
int price = 0;
if (Price.getValue() instanceof Integer) {
// Check whether the value of Price is an integer.
price = (Integer) Price.getValue();
// Cast the value of Price to an integer to prevent errors caused by inconsistent types during comparison.
} else {
price = Integer.parseInt(Utils.objectToString(Price.getValue()));
}
/**
* Check whether the price is less than the set minimum value. If so, return 1 (failure); if not, return 0 (success).
*/
if (price < MinNum) {
type = 1;
}
} else {
type = 0;
}
}
public String getMessage() {
/**
* Based on whether the validation status is 0 (success) or 1 (failure), set the corresponding returned information.
*/
if (type == 0) {
return "Validated successfully";
// When the validation is successful, the default system message "Validated successfully" will be displayed, no matter what the returned information is set here.
} else {
return "The price cannot be less than " + MinNum;
}
}
public Verifier.Status getType() {
// Return the validation status.
return Verifier.Status.parse(type);
}
public void doFinish(Calculator arg0) throws Exception {
}
}Setting Data Validation
(1) Add Custom Validation1 in the Data Entry Attribute window, and click Edit, as shown in the following figure.

(2) Copy the Java code written in the section "Custom Validation Class Editing" to the custom function editing page, and click Compile. You can only save the code after “Successfully Compiled!” is displayed, as shown in the following figure.

Note:You can download the class file by clicking VerifyDemo.zip.
(3) Click Add Attribute twice to add two attributes for the submission event. Ensure that the value types are consistent with the types defined in the code, as shown in the following figure.

Effect Display
Click Data Entry Preview, enter a value, and click Data Validation.

Note:Template Download
Download the template by clicking: Custom Validation.cpt.
Note: