antd vue + ts(vue-property-decorator) 表单至少二选一非空检验

表单验证:确保name或enName必填
该博客介绍了如何在Vue.js应用中使用A-Form-Model组件进行表单验证,确保`name`或`enName`字段至少有一个非空。通过在rules属性中使用逻辑运算符&&来动态添加required规则,并监听`addForm`对象的`name`和`enName`变化,触发校验以消除错误提示。

需求:name与enName至少二选一非空检验

<a-form-model
        ref="addForm"
        :model="addForm"
        :label-col="{ span: 24 }"
        :wrapper-col="{ span: 24 }"
        :rules="rules"
      >
        <a-form-model-item :label="$t('name')" prop="name">
          <a-auto-complete
            v-model="addForm.name"
            :data-source="dataSource"
            @select="nameSelect"
            @search="onNameSearch"
            @change="onAutoChange"
          />
        </a-form-model-item>
        <a-form-model-item :label="$t('enName')" prop="enName">
          <a-input v-model="addForm.enName" :disabled="isDisabled" />
        </a-form-model-item>
        <a-form-model-item :label="$t('email')" prop="email">
          <a-input v-model="addForm.email" :disabled="isDisabled" />
        </a-form-model-item>
        <a-form-model-item :label="$t('employeeType')" prop="employeeType">
          <a-select @change="handleChange" v-model="addForm.employeeType">
            <a-select-option value="供应商">
              {{ $t("supplier") }}
            </a-select-option>
            <a-select-option value="其他"> {{ $t("else") }} </a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
public get rules() {
    return {
      name: [
        {
          message: this.$tc("nameInvaild"),
          max: 50,
          min: 1,
          whitespace: true,
        },
        !this.addForm.enName && {
          required: true,
          message: this.$tc("nameTip"),
        },
      ],
      enName: [
        {
          message: this.$tc("enNameInvaild"),
          pattern: /^[a-zA-Z\s]{1,50}$/,
          whitespace: true,
        },
        !this.addForm.name && { required: true, message: this.$tc("nameTip") },
      ],
      email: [
        { required: true, message: this.$tc("emailInvaild"), type: "email" },
      ],
      employeeType: [{ required: true, message: this.$tc("pleaseSelect") }],
    };
  }

还需要监听一下change事件

@Watch("addForm.enName", { immediate: true, deep: true })
  public onEnNameChange(val): void {
    let item = this.$refs.addForm as any;
    item.validateField("name", (valid) => {});
  }

  @Watch("addForm.name", { immediate: true, deep: true })
  public onNameChange(val): void {
    let item = this.$refs.addForm as any;
    item.validateField("enName", (valid) => {});
  }

总结:采用 &&在rules里判断是否添加require,当某一项输入内容时,触发Watch校验另一项内容即可消除提示。

在 Ant Design Vue 3 (简称 AV) 中结合 TypeScript 使用展开收缩功能,并包含图标操作,你可以通过以下步骤来实现: 1. 首先,安装必要的依赖: - `@ant-design/icons-vue`: 提供 Ant Design 图标库的 Vue 版本 - `vue-class-component`: 类组件支持 - `vue-property-decorator`: 类属性装饰器 ```bash npm install @ant-design/icons-vue vue-class-component vue-property-decorator --save ``` 2. 定义个带有状态管理的组件,例如使用Vuex来存储展开状态: ```typescript import { defineComponent, PropType } from 'vue'; import { computed, ref } from 'vue'; import { Store } from 'vuex'; import { PlusOutlined,MinusOutlined } from '@ant-design/icons-vue'; export default defineComponent({ components: { PlusOutlined, MinusOutlined }, props: { initialExpand: { type: Boolean, default: false }, storeKey: { type: String, required: true } }, setup(props, { emit }) { const store = ref<Store>(); const isExpanded = ref(props.initialExpand); // 连接到 Vuex 的 store Object.defineProperty(store.value, 'commit', { get: () => (action: string, payload?: any) => this.$store.commit(action, payload), configurable: true }); const toggleExpand = () => { isExpanded.value = !isExpanded.value; store.value.commit('toggle', props.storeKey); }; return { isExpanded: computed(() => isExpanded.value), expandIcon: isExpanded.value ? <MinusOutlined /> : <PlusOutlined />, toggleExpand }; } }); ``` 3.Vue 组件中使用这个自定义组件,并绑定 Vuex 状态: ```html <template> <div> <button @click="toggleExpand"> {{ isExpanded ? '折叠' : '展开' }} <Icon v-if="isExpanded" :type="expandIcon.type" /> </button> </div> </template> <script lang="ts"> import { Component, Prop } from 'vue'; import { Store } from 'vuex'; import CustomComponent from './CustomComponent.vue'; interface Props extends ComponentProps<typeof CustomComponent> { store: Store<any>; } export default { components: { CustomComponent }, props: { ...CustomComponent.props, store: { type: Store, required: true } }, setup(props) { const { toggleExpand, isExpanded, expandIcon } = props.customComponent; return { ...props, toggleExpand, isExpanded, expandIcon }; } }; </script> ``` 4.Vuex store 中处理状态的切换: ```javascript import Vue from 'vue'; import Vuex from 'vuex'; const store = new Vuex.Store({ state: { [props.storeKey]: false, }, mutations: { toggle(state, key) { state[key] = !state[key]; }, }, }); export default store; ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值