Skip to content

Form Validation (New)

We've built in a new form validation mechanism. Through the built-in validation engine, it automatically executes validation logic based on component value types and adapts to all UI versions, solving the problem of the original solution requiring specialized adaptation for different data types and UI frameworks, significantly reducing developers' configuration burden and complexity.

Validation Rules

RuleValidation LogicExample
adapterEnable new validation logic, fixed as trueadapter: true
requiredRequiredrequired:true → "hello" (pass)
lenValue length must equal specified numberlen:5 → "hello" (pass)
maxLenValue length does not exceed specified numbermaxLen:10 → "abc" (pass)
minLenValue length not less than specified numberminLen:3 → "ab" (fail)
patternValue must match regular expression (supports string or RegExp object)pattern:^\d+$ → "123" (pass)
uppercaseString must be all uppercaseuppercase:true → "ABC" (pass)
lowercaseString must be all lowercaselowercase:true → "abc" (pass)
emailMust conform to email formatemail:true → "a@b.com" (pass)
urlMust conform to URL format (excluding mailto:)url:true → "https://example.com"
ipMust conform to IPv4 address formatip:true → "192.168.1.1" (pass)
phoneMust conform to phone number format (supports +86 prefix)phone:true → "13800138000" (pass)
minNumeric value not less than specified valuemin:10 → 15 (pass)
maxNumeric value does not exceed specified valuemax:100 → 50 (pass)
positiveMust be positive number (>0)positive:true → 1 (pass)
negativeMust be negative number (<0)negative:true → -1 (pass)
integerMust be integerinteger:true → 3.0 (pass)
numberMust be valid number (not NaN)number:true → "123" (pass)
equalValue must equal specified value (strict match ===)equal:"abc" → "abc" (pass)
enumValue must be in specified arrayenum:[1,2,3] → 2 (pass)
hasKeysObject must contain specified key names (only validates key existence, not values)hasKeys:["id"]{id:1} (pass)
validatorCustom validation logic, must call callback, this.api can get form apivalidator:(value, callback) => void;
messageError messagemessage:"Fill in error"
message:{default:"Fill in error", min:"Cannot be less than 5 digits" }

Note

In custom validation, you can obtain component and form information through this.rule and this.api, making it convenient to access values of other components during validation.

When pattern type is string, it cannot contain / symbols before and after, otherwise the regular expression will be invalid.

In custom validation, whether successful or failed, the callback function must be executed.

Common Validation Examples

Input Value Must Be Greater Than 10

js
const rule = [
    {
        type: 'input',
        field: 'age',
        title: 'Age',
        //value is string
        validate: [
            { adapter: true, min: 10, message: 'Age must be greater than 10' }
        ]
    },{
        type: 'inputNumber',
        field: 'age2',
        title: 'Age',
        //value is number
        validate: [
            { adapter: true, min: 10, message: 'Age must be greater than 10' }
        ]
    }
]

Length Must Be Greater Than 3

js
const rule = [
    {
        type: 'input',
        field: 'code',
        title: 'Code',
        //value is string
        validate: [
            { adapter: true, min: 3, max: 5, message: 'Code must be 3-5 digits' }
        ]
    },{
        type: 'inputNumber',
        field: 'code2',
        title: 'Code2',
        //value is number
        validate: [
            { adapter: true, min: 3, max: 5, message: {
                min: 'Code must be greater than or equal to 3 digits',
                max: 'Code must be less than or equal to 5 digits'
            }}
        ]
    },{
        type: 'group',
        field: 'codes',
        title: 'Code3',
        //value is array
        props: {
            rule: [{
                type: 'input',
                field: 'code',
                title: 'Code',
            }]  
        },
        validate: [
            { adapter: true, min: 3, max: 5, message: 'Code must be 3-5 digits' }
        ]
    }
]

Custom Validation

js
const rule = [
    {
        type: 'input',
        field: 'code',
        title: 'Code',
        validate: [
            { adapter: true, validator(value, callback){
                  if(value.length >= 3 && value.length <= 5) {
                      callback();
                  } else {
                      callback('Code must be 3-5 digits');
                  }
                }
            }
        ]
    }
]

Validation Combined with Other Fields

js
const rule = [
    {
        type: 'input',
        field: 'code',
        title: 'Code',
        validate: [
            { adapter: true, validator(value, callback){
                  if(this.api.getValue('code2') === value) {
                      callback();
                  } else {
                      callback('Codes do not match');
                  }
                }
            }
        ]
    },
    {
        type: 'input',
        field: 'code2',
        title: 'Code',
    },
]

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.