formSubmit(e) {
//e.detail.value[${item}]
console.log(e);
let formObj = e.detail.value;
let flag = false;
//表单校验
try {
this.data.extendFieldList.forEach((value, index, array) => {
if (value.required == "1") {
if (!(formObj[value.extendFieldId] && formObj[value.extendFieldId].length > 0)) {
// 条件成功,抛出错误
throw new Error(`请填写${value.fieldName}`)
} else {
// flag = true;
}
}
})
} catch (e) {
console.log('foreach error', e.message)
this.setData({
tips: e.message,
})
flag = true;
}
// let flag = this.data.extendFieldList.every((value, index, array) => {
// if (value.required == "1") {
// if (!(formObj[value.extendFieldId] && formObj[value.extendFieldId].length > 0)) {
// this.setData({
// tips: `请填写${value.fieldName}`,
// })
// } else {
// return true;
// }
// // return formObj[value.extendFieldId] && formObj[value.extendFieldId].length > 0
// }
// })
//保存 提交form表单和用户id=app.globalData.userInfo.userId bizId
if (!flag) {
console.log("校验成功");
let extendFieldDataEntityList = [];
Object.entries(formObj).forEach(function ([id, value]) {
extendFieldDataEntityList.push({
"fieldValue": value,
"extendFieldId": id,
"bizId": app.globalData.userInfo.userId
});
});
console.log(extendFieldDataEntityList);
//传给后台
}
},
动态表单校验
<form class="form-content" bindsubmit="formSubmit" bindreset="formReset">
<t-cell-group>
<t-cell wx:for="{{extendFieldList}}" wx:for-item="item" wx:key="index" wx:for-index="index" class="form-cell" t-class-title="t-cell-title" title="{{item.fieldName}}" t-class-note="t-cell-note" required="{{item.required=='1'?true:false}}">
<t-input name="{{item.extendFieldId}}" class="t-input" slot="note" t-class="field-text" borderless="{{item.fieldName}}" maxlength="{{item.maxLength}}" type="text" placeholder="{{'请输入'+item.fieldName}}" bind:change="onInputValue" data-type="{{item.required}}" />
</t-cell>
</t-cell-group>
<!-- <t-cell-group>
<t-cell class="form-cell" t-class-title="t-cell-title" title="收货人" t-class-note="t-cell-note">
<t-input class="t-input" slot="note" t-class="field-text" borderless data-item="name" maxlength="20" type="text" value="{{locationState.name}}" placeholder="您的姓名" bind:change="onInputValue" />
</t-cell>
<t-cell class="form-cell" t-class-title="t-cell-title" title="手机号">
<t-input slot="note" class="t-input" t-class="field-text" borderless type="number" value="{{locationState.phone}}" maxlength="11" placeholder="联系您的手机号" bind:change="onInputValue" data-item="phone" />
</t-cell>
<t-cell class="form-cell" t-class-title="t-cell-title" title="地区">
<t-input slot="note" class="t-input" t-class="field-text" borderless placeholder="省/市/区" data-item="address" value="{{locationState.provinceName ? locationState.provinceName+'/':'' }}{{locationState.cityName ? locationState.cityName+'/':''}}{{locationState.districtName}}" catch:tap="onPickArea" disabled />
<t-icon slot="right-icon" t-class="map" prefix="wr" name="location" catch:tap="onSearchAddress" />
</t-cell>
<t-cell class="form-cell" t-class-title="t-cell-title" title="详细地址" bordered="{{false}}">
<view slot="note" class="textarea__wrapper">
<t-textarea slot="note" type="text" value="{{locationState.detailAddress}}" placeholder="门牌号等(例如:10栋1001号)" autosize bind:change="onInputValue" data-item="detailAddress" />
</view>
</t-cell>
<view class="divider-line" />
<t-cell class="form-cell" t-class-note="t-cell-note address__tag" t-class-title="t-cell-title" title="标签" bordered="{{false}}">
<view class="t-input address-flex-box" slot="note">
<t-button wx:for="{{labels}}" wx:for-item="label" wx:key="index" size="extra-small" t-class="label-list {{locationState.labelIndex === index ? 'active-btn':''}}" bindtap="onPickLabels" data-item="{{index}}">
{{label.name}}
</t-button>
<t-button size="extra-small" t-class="label-list" bindtap="addLabels">
<t-icon name="add" size="40rpx" color="#bbb" />
</t-button>
</view>
</t-cell>
<view class="divider-line" />
<t-cell title="设置为默认收货地址" bordered="{{false}}">
<t-switch value="{{locationState.isDefault}}" slot="note" colors="{{['#0ABF5B', '#c6c6c6']}}" bind:change="onCheckDefaultAddress" />
</t-cell>
</t-cell-group> -->
<view class="submit">
<!-- <t-button shape="round" block disabled="{{!submitActive}}" bind:tap="formSubmit"> 保存 </t-button> -->
<t-button shape="round" block type="submit">提交</t-button>
</view>
</form>