iview自定义表单校验规则

iview自定义表单校验规则_第1张图片iview自定义表单校验规则_第2张图片

export default {
	data () {
		return {
			rules: {
                NodeNameCH: [
                    {required: true, message: '中文名称不能为空', trigger: 'blur'},
                    {validator:(rule, value, cb)=>{this.isNodeNameExist(value, cb)}, trigger: 'change'}
                ],
                nodeInsertIndex: [
                    {required: true, type: "number", message: '节点插入位置不能为空', trigger: 'change'},
                ]
		}
	},
	methods: {
		isNodeNameEnExist(value, cb, index = this.updateIndex){
            for(let i=0; i<this.nodesData.length; i++){
                let item = this.nodesData[i]
                if(item.NodeNameEN.trim() === value.trim()){
                    if(!index || (index && i!==index)){
                        return cb(new Error('节点名称已存在'))
                    }
                }
            }
            cb()
        },
	}
}

你可能感兴趣的:(vue)