<template>
  <el-form-item
    :label="labelWidth == '0px' ? '' : item.name"
    :label-width="labelWidth"
    :rules="item.required ? {required: item.required, validator: (rule, value, callback) => onValidate(rule, value, callback, item), trigger: ['change']} : null">
    <slot></slot>
  </el-form-item>
</template>

<script>

export default {
  props: ['item', 'labelWidth'],
  data() {
    return {}
  },
  methods: {
    onValidate(rule, value, callback, item) {
      if (value === '' || value == null) {
        callback(new Error(item.errMsg))
      } else {
        if (item.dataType === 1) {
          if (value.length > item.length || !new RegExp(item.regEx).test(value)) callback(new Error(item.errMsg))
        } else if ([2, 3].includes(item.dataType)) {
          if (value < item.min || value > max) callback(new Error(item.errMsg))
        }
        callback()
      }
    }
  },
  created() {

  }
}
</script>

<style scoped>

</style>
