Custom Validation

  • Last update:December 10, 2025
  • 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.

    1.2 功能入口.png

    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.

    数据准备.png

    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.

    设计表格.png

    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.4 设置数据校验(1.png

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

    2.4 设置数据校验(2.png

    iconNote:
    You can also place the externally compiled class file to a folder in FineReport installation directory\webapps\webroot\WEB-INF\classes that is the same as that in the package path defined in the code, and then click Select to reference the class file. If the class file is modified or replaced, you need to restart the designer or FanRuan service for the modified code to take effect.

    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.

    2.4 设置数据校验(3.png

    Effect Display

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

    效果预览.gif

    iconNote:
    The effect can be previewed on mobile terminals.

    Template Download

    Download the template by clicking: Custom Validation.cpt.

    iconNote:
    After you have downloaded the template, you need to follow the operations in the section "Setting Data Validation" to add a data validation event for the custom validation to take effect.

    Attachment List


    Theme: Data Entry
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy