| | |
| | | |
| | | <template> |
| | | <view class="c-form-item"> |
| | | <slot :itemData="itemData" /> |
| | | <slot :itemRes="itemRes" /> |
| | | </view> |
| | | </template> |
| | | |
| | |
| | | label: String, |
| | | required: Boolean, |
| | | rules: Array, |
| | | formData: Object, |
| | | formRes: Object, |
| | | }, |
| | | data() { |
| | | return { |
| | |
| | | }; |
| | | }, |
| | | computed: { |
| | | itemData() { |
| | | itemRes() { |
| | | return { |
| | | formData: this.formData, |
| | | formData: this.formRes.formData, |
| | | name: this.name, |
| | | label: this.label, |
| | | required: this.isRequired, |
| | |
| | | } |
| | | } |
| | | // 未改变值不触发 |
| | | if (this.formData[this.name] === evt) { |
| | | if (this.formRes.formData[this.name] === evt) { |
| | | return; |
| | | } |
| | | this.formData.$handleChange({ |
| | | this.formRes.$handleChange({ |
| | | [this.name]: evt, |
| | | }); |
| | | }, |
| | |
| | | this.$nextTick(() => { |
| | | // 未设置验证 |
| | | if (!this.required && !this.rules) { |
| | | this.formData.$regItemValidator(this.name, () => { |
| | | this.formRes.$regItemValidator(this.name, (validateType) => { |
| | | this.error = false; |
| | | return Promise.resolve({ |
| | | name: this.name, |
| | |
| | | validator.messages(validateMsgs); |
| | | // 注册验证 |
| | | let errTimer = null; |
| | | this.formData.$regItemValidator(this.name, () => { |
| | | this.formRes.$regItemValidator(this.name, (validateType) => { |
| | | return validator |
| | | .validate({ |
| | | [this.name]: this.formData[this.name], |
| | | [this.name]: this.formRes.formData[this.name], |
| | | }) |
| | | .then( |
| | | (res) => { |
| | |
| | | }; |
| | | }, |
| | | ({ errors, fields }) => { |
| | | this.error = true; |
| | | clearTimeout(errTimer); |
| | | errTimer = setTimeout(() => { |
| | | this.error = false; |
| | | }, 5000); |
| | | if (validateType !== 'msgOnly') { |
| | | this.error = true; |
| | | clearTimeout(errTimer); |
| | | errTimer = setTimeout(() => { |
| | | this.error = false; |
| | | }, 5000); |
| | | } |
| | | return { |
| | | name: this.name, |
| | | passed: false, |
| | |
| | | }); |
| | | }, |
| | | beforeDestroy() { |
| | | this.formData.$regItemValidator(this.name, null); |
| | | this.formRes.$regItemValidator(this.name, null); |
| | | }, |
| | | }; |
| | | </script> |