foreach 返回+every使用

 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>

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值