| | |
| | | */ |
| | | |
| | | <template> |
| | | <form |
| | | class="c-form" |
| | | @submit="evt=>handleSubmit()" |
| | | > |
| | | <slot :formRes="formRes" /> |
| | | </form> |
| | | <view :class="'c-form-outer ' + (autoScrollToError==='on'?'auto-scroll':'')"> |
| | | <form |
| | | class="c-form" |
| | | v-if="autoScrollToError==='off'" |
| | | @submit="evt=>handleSubmit()" |
| | | > |
| | | <slot :formRes="formRes" /> |
| | | </form> |
| | | <scroll-view |
| | | class="c-form-scroller" |
| | | v-else-if="autoScrollToError==='on'" |
| | | :scrollY="true" |
| | | :scroll-top="scrollTop" |
| | | :scroll-with-animation="true" |
| | | @scroll="evt => handleScroll(evt)" |
| | | > |
| | | <form |
| | | class="c-form" |
| | | @submit="evt=>handleSubmit()" |
| | | > |
| | | <slot :formRes="formRes" /> |
| | | </form> |
| | | </scroll-view> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | |
| | | props: { |
| | | // 表单数据主体 |
| | | formData: Object, |
| | | // 是否自动滚动到错误位置,on/off |
| | | autoScrollToError: { |
| | | type: String, |
| | | default: 'off', |
| | | }, |
| | | // 表单项变化的回调 |
| | | // 回调传参只有单项数据 |
| | | onChange: Function, |
| | |
| | | formData: this.formData, |
| | | }, |
| | | validators: {}, |
| | | scrollTop: '', |
| | | }; |
| | | }, |
| | | methods: { |
| | | handleScroll(evt) { |
| | | this.scrollTop = evt.target.scrollTop; |
| | | }, |
| | | handleSubmit() { |
| | | const checklist = []; |
| | | // 所有项验证 |
| | |
| | | for (let validation of validations) { |
| | | // 第一个不通过项提示 |
| | | if (!validation.passed) { |
| | | if (this.autoScrollToError === 'on') { |
| | | this.scrollTop = validation.offset.top; |
| | | } |
| | | Taro.showToast({ |
| | | title: validation.msg, |
| | | icon: 'none', |
| | |
| | | this.onFinish && this.onFinish(); |
| | | }); |
| | | }, |
| | | preCheckValidators(keys, callback) { |
| | | // 手工提交 |
| | | $submit() { |
| | | this.handleSubmit(); |
| | | }, |
| | | // 提前验证指定项 |
| | | $preVerify(keys, callback) { |
| | | const checklist = []; |
| | | // 从选定的项中验证 |
| | | keys.forEach(key => { |
| | |
| | | for (let validation of validations) { |
| | | // 第一个不通过项提示 |
| | | if (!validation.passed) { |
| | | if (this.autoScrollToError === 'on') { |
| | | this.scrollTop = validation.offset.top; |
| | | } |
| | | Taro.showToast({ |
| | | title: validation.msg, |
| | | icon: 'none', |
| | |
| | | callback && callback(true); |
| | | }); |
| | | }, |
| | | $submit() { |
| | | this.handleSubmit(); |
| | | }, |
| | | $preVerify(keys, callback) { |
| | | this.preCheckValidators(keys, callback); |
| | | // 直接设定错误 |
| | | $setErrors(errors) { |
| | | const checklist = []; |
| | | errors.forEach(errorItem => { |
| | | if (typeof this.validators[errorItem.key] !== 'undefined') { |
| | | checklist.push( |
| | | this.validators[errorItem.key]('setError', errorItem.msg) |
| | | ); |
| | | } |
| | | }); |
| | | Promise.all(checklist).then(validations => { |
| | | for (let validation of validations) { |
| | | // 第一个不通过项提示 |
| | | if (!validation.passed) { |
| | | if (this.autoScrollToError === 'on') { |
| | | this.scrollTop = validation.offset.top; |
| | | } |
| | | Taro.showToast({ |
| | | title: validation.msg, |
| | | icon: 'none', |
| | | mask: false, |
| | | duration: 2000, |
| | | }); |
| | | return; |
| | | } |
| | | } |
| | | }); |
| | | }, |
| | | }, |
| | | mounted() { |