<commit_before><template>
    <div>
        <label v-if="label">{{label}}</label>
        <slot></slot>
    </div>
</template>
<script>
import AsyncValidator from 'async-validator';
import emit from '../../mixins/emit.js';

export default {
    name: 'vutFormItem',
    reject: ['form'],
    props: {
        label: {
            type: String,
            default: ''
        },
        prop: {
            type: String
        }
    },
    data() {
        return {
            validateStatus: '',
            validateMessage: ''
        }
    },
    mixins: [ emit ],
    computed: {
        fieldValue() {
            return this.form.model[this.prop]
        }
    },
    mounted() {
        this.dispatch('vutForm', 'on-form-item-add', this);
        this.$on('on-change-item', this.handleFieldChange);
        this.$on('on-blur-item', this.handleFieldBlur);
    },
    destroyed() {
        this.dispatch('vutForm', 'on-form-item-remove', this);
    },
    methods: {
        getRules() {
            let formRules = this.props.form.rules;
            itemRules = formRules ? formRules[this.prop] : []
            return [].concat(formRules || []);
        },
        filterTriggerRule(trigger) {
            return this.getRules().filter(r => {
                return !r.trigger || r.trigger.index(trigger) !== -1
            })
        },
        validate(trigger, callback = function() {}) {
            const rules = this.filterTriggerRule(trigger);

            if (!rules || rules.length === 0) {
                return true;
            }

            this.validateStatus = 'validating';
            let descriptor = {};
            descriptor[this.prop] = rules;
            const validator = new AsyncValidator(descriptor);
            let model = {};
            model[this.prop] = this.fieldValue;
            validator.validate(model, {firstField: true}, error => {
                this.validateStatus = !errors ? 'success' : 'error';
                this.validateMessage = errors ? errors[0].message : '';

                callback(this.validateMessage);
            })
        },
        handleFieldChange(value) {
            this.validate('input');
        },
        handleFieldBlur(value) {
            this.validate('blur');
        }
    }
}
</script><commit_msg>[formItem] 增加require状态及重置方法<commit_after><template>
    <div>
        <label v-if="label" :class="{'vut-form-item-required': required}" >{{label}}</label>
        <slot></slot>
        <div v-if="validateStatus === 'error'">{{validateMessage}}</div>
    </div>
</template>
<script>
import AsyncValidator from 'async-validator';
import emit from '../../mixins/emit.js';

export default {
    name: 'vutFormItem',
    reject: ['form'],
    props: {
        label: {
            type: String,
            default: ''
        },
        prop: {
            type: String
        }
    },
    data() {
        return {
            initialValue: '',
            required: false,
            validateStatus: '',
            validateMessage: ''
        }
    },
    mixins: [ emit ],
    computed: {
        fieldValue() {
            return this.form.model[this.prop]
        }
    },
    mounted() {
        this.dispatch('vutForm', 'on-form-item-add', this);
        this.initialValue = this.fieldValue;
        this.initRules();
    },
    destroyed() {
        this.dispatch('vutForm', 'on-form-item-remove', this);
    },
    methods: {
        // 初始化监听校验
        initRules() {
            const rules = this.getRules();
            if (rules.length) {
                rules.some(rule => {
                    if (rule.required) {
                        this.required = true;
                        return true;
                    }
                })
            }
            this.$on('on-change-item', this.handleFieldChange);
            this.$on('on-blur-item', this.handleFieldBlur);
        },
        getRules() {
            let formRules = this.props.form.rules;
            itemRules = formRules ? formRules[this.prop] : []
            return [].concat(formRules || []);
        },
        filterTriggerRule(trigger) {
            return this.getRules().filter(r => {
                return !r.trigger || r.trigger.index(trigger) !== -1
            })
        },
        validate(trigger, callback = function() {}) {
            const rules = this.filterTriggerRule(trigger);

            if (!rules || rules.length === 0) {
                return true;
            }

            this.validateStatus = 'validating';
            let descriptor = {};
            descriptor[this.prop] = rules;
            const validator = new AsyncValidator(descriptor);
            let model = {};
            model[this.prop] = this.fieldValue;
            validator.validate(model, {firstField: true}, error => {
                this.validateStatus = !errors ? 'success' : 'error';
                this.validateMessage = errors ? errors[0].message : '';

                callback(this.validateMessage);
            })
        },
        // 重置数据及校验状态
        resetField() {
            this.validateStatus = '';
            this.validateMessage = '';
            this.form.model[this.prop] = this.initialValue;
        },
        handleFieldChange(value) {
            this.validate('input');
        },
        handleFieldBlur(value) {
            this.validate('blur');
        }
    }
}
</script>
{'fraction_non_alphanumeric': 0.14558960074280408, 'fraction_numerical': 0.0011142061281337048, 'mean_word_length': 1.5333960489181562, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5303128', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1471, 'n_words': 256}