The iview form verifies whether a file can be separated.

problem description

can you extract the rules for iview form validation and write a public one, citing

when you use it?

the environmental background of the problems and what methods you have tried

clipboard.png

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

May.05,2022

can
you can extract into a utils/validate file for public form validation. Implement validation configuration and customize form validation
for example:

// validate.js
export const requiredFn = function (message = 'required', trigger = 'blur') {
    return [
        { required: true, message, trigger }
    ]
}
export const typeFn = function (type = 'text', message = 'some text', trigger = 'blur') {
    return [
        {type, message, trigger}
    ]
}

// form.vue
import { typeFn, requiredFn } from 'validate.js'
ruleValidate1: {
    FirstName: requiredFn('The name cannot be empty'),
    Email: [
        ...requiredFn('Mailbox cannot be empty'),
        ...typeFn('email', 'Incorrect email format')
    ]
}

the last project took this problem into account when using ElementUI, so a verification component validate.js was extracted in this project.
for example, if you need to verify the code in Chinese and English:

masp_no: [
          {
            required: true,
            validator: checkCode,
            trigger: ['blur', 'change']
          }
        ]

then the custom validation goes like this

const checkCode = (rule, value, callback) => {
      let check = this.$validate({label: '', value, rules: ['notnull', 'string']})
      if (!check.result) {
        callback(new Error(check.message))
      } else {
        callback()
      }
    }

verify that the function is defined in this way

/**
 * 
 * @param {Obj} { label, value, rules, conditions}
 *  @param {String} label: 
 *  @param {String} value:  (value1)
 *  @param {Array} rules:  : ['notnull', 'length'] notnull
 *  @param {Array} conditions:  : ['2', '10'] ,: 210,, {1}
 * @return {obj} { result, message } 
 */
function validate(obj) {
    return {result, message}
}

verification function is here. Click to see

.
Menu