国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Element Plus Form 動態(tài)表單自定義校驗規(guī)則使用教程

這篇具有很好參考價值的文章主要介紹了Element Plus Form 動態(tài)表單自定義校驗規(guī)則使用教程。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Element Plus,基于 Vue 3,面向設(shè)計師和開發(fā)者的組件庫

Element Plus 官網(wǎng):https://element-plus.org/zh-CN/

Element Plus Form 動態(tài)表單自定義效驗規(guī)則,官網(wǎng)示例代碼中沒有,官網(wǎng)示例中的動態(tài)表單是固定的規(guī)則,本文講解動態(tài)表單自定義規(guī)則的使用

目錄

1、官網(wǎng)動態(tài)表單示例代碼

2、表單自定義規(guī)則

3、動態(tài)表單自定義規(guī)則

3.1、單字段動態(tài)表單

3.2、多字段動態(tài)表單

4、總結(jié)


1、官網(wǎng)動態(tài)表單示例代碼

先看官網(wǎng)動態(tài)表單示例代碼

<template>
  <el-form
    ref="formRef"
    :model="dynamicValidateForm"
    label-width="120px"
    class="demo-dynamic"
  >
    <el-form-item
      prop="email"
      label="Email"
      :rules="[
        {
          required: true,
          message: 'Please input email address',
          trigger: 'blur',
        },
        {
          type: 'email',
          message: 'Please input correct email address',
          trigger: ['blur', 'change'],
        },
      ]"
    >
      <el-input v-model="dynamicValidateForm.email" />
    </el-form-item>
    
    <el-form-item
      v-for="(domain, index) in dynamicValidateForm.domains"
      :key="domain.key"
      :label="'Domain' + index"
      :prop="'domains.' + index + '.value'"
      :rules="{
        required: true,
        message: 'domain can not be null',
        trigger: 'blur',
      }"
    >
      <el-input v-model="domain.value" />
      <el-button class="mt-2" @click.prevent="removeDomain(domain)"
        >Delete</el-button
      >
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(formRef)">Submit</el-button>
      <el-button @click="addDomain">New domain</el-button>
      <el-button @click="resetForm(formRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'

const formRef = ref<FormInstance>()
const dynamicValidateForm = reactive<{
  domains: DomainItem[]
  email: string
}>({
  domains: [
    {
      key: 1,
      value: '',
    },
  ],
  email: '',
})

interface DomainItem {
  key: number
  value: string
}

const removeDomain = (item: DomainItem) => {
  const index = dynamicValidateForm.domains.indexOf(item)
  if (index !== -1) {
    dynamicValidateForm.domains.splice(index, 1)
  }
}

const addDomain = () => {
  dynamicValidateForm.domains.push({
    key: Date.now(),
    value: '',
  })
}

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

運(yùn)行效果

Element Plus Form 動態(tài)表單自定義校驗規(guī)則使用教程,Element UI,vue.js,elementui,javascript,view design

這個是把?rules 寫死在?el-form-item 標(biāo)簽上的

2、表單自定義規(guī)則

官網(wǎng)的自定義規(guī)則是在?el-form 標(biāo)簽上添加?rules 屬性,并在?rules 屬性中定義自定義規(guī)則,el-form-item 標(biāo)簽通過 prop 屬性對應(yīng)找到規(guī)則

官網(wǎng)示例代碼

<template>
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
  >
    <el-form-item label="Password" prop="pass">
      <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="Confirm" prop="checkPass">
      <el-input
        v-model="ruleForm.checkPass"
        type="password"
        autocomplete="off"
      />
    </el-form-item>
    <el-form-item label="Age" prop="age">
      <el-input v-model.number="ruleForm.age" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)"
        >Submit</el-button
      >
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const checkAge = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('Please input the age'))
  }
  setTimeout(() => {
    if (!Number.isInteger(value)) {
      callback(new Error('Please input digits'))
    } else {
      if (value < 18) {
        callback(new Error('Age must be greater than 18'))
      } else {
        callback()
      }
    }
  }, 1000)
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  } else {
    if (ruleForm.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass', () => null)
    }
    callback()
  }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password again'))
  } else if (value !== ruleForm.pass) {
    callback(new Error("Two inputs don't match!"))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  pass: '',
  checkPass: '',
  age: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  pass: [{ validator: validatePass, trigger: 'blur' }],
  checkPass: [{ validator: validatePass2, trigger: 'blur' }],
  age: [{ validator: checkAge, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

運(yùn)行效果

Element Plus Form 動態(tài)表單自定義校驗規(guī)則使用教程,Element UI,vue.js,elementui,javascript,view design

3、動態(tài)表單自定義規(guī)則

動態(tài)表單自定義規(guī)則就是將動態(tài)表單和自定義規(guī)則結(jié)合,在?el-form-item 標(biāo)簽屬性?rules 上添加自定義規(guī)則

3.1、單字段動態(tài)表單

看下面代碼

<template>
  <el-form
    ref="formRef"
    :model="dynamicValidateForm"
    label-width="120px"
    class="demo-dynamic"
  >
    <el-form-item
      prop="email"
      label="Email"
      :rules="[
        {
          required: true,
          message: 'Please input email address',
          trigger: 'blur',
        },
        {
          type: 'email',
          message: 'Please input correct email address',
          trigger: ['blur', 'change'],
        },
      ]"
    >
      <el-input v-model="dynamicValidateForm.email" />
    </el-form-item>
    <el-form-item
      v-for="(domain, index) in dynamicValidateForm.domains"
      :key="domain.key"
      :label="'Domain' + index"
      :prop="'domains.' + index + '.value'"
      :rules="[
        {
          required: true,
          message: 'domain can not be null',
          trigger: 'blur',
        },
        { validator: validatePass, trigger: 'blur' }
      ]"
    >
      <el-input v-model="domain.value" />
      <el-button class="mt-2" @click.prevent="removeDomain(domain)"
        >Delete</el-button
      >
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(formRef)">Submit</el-button>
      <el-button @click="addDomain">New domain</el-button>
      <el-button @click="resetForm(formRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'

const formRef = ref<FormInstance>()
const dynamicValidateForm = reactive<{
  domains: DomainItem[]
  email: string
}>({
  domains: [
    {
      key: 1,
      value: '',
    },
  ],
  email: '',
})

interface DomainItem {
  key: number
  value: string
}

const removeDomain = (item: DomainItem) => {
  const index = dynamicValidateForm.domains.indexOf(item)
  if (index !== -1) {
    dynamicValidateForm.domains.splice(index, 1)
  }
}

const addDomain = () => {
  dynamicValidateForm.domains.push({
    key: Date.now(),
    value: '',
  })
}

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  } else {
    if (value.length > 5) {
      callback(new Error('密碼長度不能超過5'))
    }
    callback()
  }
}
</script>

運(yùn)行效果

Element Plus Form 動態(tài)表單自定義校驗規(guī)則使用教程,Element UI,vue.js,elementui,javascript,view design

3.2、多字段動態(tài)表單

<template>
  <el-form
    ref="formRef"
    :model="dynamicValidateForm"
    label-width="120px"
    class="demo-dynamic"
  >
    <el-form-item
      prop="email"
      label="Email"
      :rules="[
        {
          required: true,
          message: 'Please input email address',
          trigger: 'blur',
        },
        {
          type: 'email',
          message: 'Please input correct email address',
          trigger: ['blur', 'change'],
        },
      ]"
    >
      <el-input v-model="dynamicValidateForm.email" />
    </el-form-item>

    <template v-for="(domain, index) in dynamicValidateForm.domains" :key="domain.key">
      <el-form-item
        :label="'Domain' + index"
        :prop="'domains.' + index + '.value'"
        :rules="[
          {
            required: true,
            message: 'domain can not be null',
            trigger: 'blur',
          },
          { validator: validatePass, trigger: 'blur' }
        ]"
      >
        <el-input v-model="domain.value" />
      </el-form-item>
      <el-form-item
        :label="'Name' + index"
        :prop="'domains.' + index + '.name'"
        :rules="[
          {
            required: true,
            message: 'domain can not be null',
            trigger: 'blur',
          },
          { validator: validateName, trigger: 'blur' }
        ]"
      >
        <el-input v-model="domain.name" />
        <el-button class="mt-2" @click.prevent="removeDomain(domain)">Delete</el-button>
      </el-form-item>
  </template>
    
    <el-form-item>
      <el-button type="primary" @click="submitForm(formRef)">Submit</el-button>
      <el-button @click="addDomain">New domain</el-button>
      <el-button @click="resetForm(formRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'

const formRef = ref<FormInstance>()
const dynamicValidateForm = reactive<{
  domains: DomainItem[]
  email: string
}>({
  domains: [
    {
      key: 1,
      value: '',
      name: ''
    },
  ],
  email: '',
})

interface DomainItem {
  key: number
  value: string
}

const removeDomain = (item: DomainItem) => {
  const index = dynamicValidateForm.domains.indexOf(item)
  if (index !== -1) {
    dynamicValidateForm.domains.splice(index, 1)
  }
}

const addDomain = () => {
  dynamicValidateForm.domains.push({
    key: Date.now(),
    value: '',
    name: '',
  })
}

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  } else {
    if (value.length > 5) {
      callback(new Error('密碼長度不能超過5'))
    }
    callback()
  }
}
const validateName = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('name 不能為空'))
  } else {
    if (value.length > 10) {
      callback(new Error('name長度不能超過10'))
    }
    callback()
  }
}
</script>

運(yùn)行效果

Element Plus Form 動態(tài)表單自定義校驗規(guī)則使用教程,Element UI,vue.js,elementui,javascript,view design

4、總結(jié)

動態(tài)表單自定義效驗規(guī)則,是將官網(wǎng)?rules 對象改成數(shù)組后,添加自定義規(guī)則即可,這種方式同樣適用于?View UI Plus,下面是??View UI Plus 代碼

<template>
  <Form ref="formDynamic" :model="formDynamic" :label-width="80" style="width: 300px">
      <template v-for="(item, index) in formDynamic.items">
        <FormItem
                v-if="item.status"
                :key="index"
                :label="'Item ' + item.index"
                :prop="'items.' + index + '.value'"
                :rules="[
                  {required: true, message: 'Item ' + item.index +' can not be empty', trigger: 'blur'},
                  { validator: validatePassCheck, trigger: 'blur' }
                ]">
            <Row>
                <Col span="18">
                    <Input type="text" v-model="item.value" placeholder="Enter something..."></Input>
                </Col>
                <Col span="4" offset="1">
                    <Button @click="handleRemove(index)">Delete</Button>
                </Col>
            </Row>
        </FormItem>
      </template>
      <FormItem>
          <Row>
              <Col span="12">
                  <Button type="dashed" long @click="handleAdd" icon="md-add">Add item</Button>
              </Col>
          </Row>
      </FormItem>
      <FormItem>
          <Button type="primary" @click="handleSubmit('formDynamic')">Submit</Button>
          <Button @click="handleReset('formDynamic')" style="margin-left: 8px">Reset</Button>
      </FormItem>
  </Form>
</template>
<script>
  export default {
      data () {
          return {
              index: 1,
              formDynamic: {
                  items: [
                      {
                          value: '',
                          index: 1,
                          status: 1
                      }
                  ]
              }
          }
      },
      methods: {
          handleSubmit (name) {
              this.$refs[name].validate((valid) => {
                  if (valid) {
                      this.$Message.success('Success!');
                  } else {
                      this.$Message.error('Fail!');
                  }
              })
          },
          handleReset (name) {
              this.$refs[name].resetFields();
          },
          handleAdd () {
              this.index++;
              this.formDynamic.items.push({
                  value: '',
                  index: this.index,
                  status: 1
              });
          },
          handleRemove (index) {
              this.formDynamic.items[index].status = 0;
          },
          validatePassCheck(rule, value, callback) {
            if (value === '') {
              callback(new Error('Please enter your password again'));
            } else if (value.length > 5) {
              callback(new Error('密碼長度不能大于5'));
            } else {
              callback();
            }
          }
      }
  }
</script>

運(yùn)行效果

Element Plus Form 動態(tài)表單自定義校驗規(guī)則使用教程,Element UI,vue.js,elementui,javascript,view design

至此完文章來源地址http://www.zghlxwxcb.cn/news/detail-800453.html

到了這里,關(guān)于Element Plus Form 動態(tài)表單自定義校驗規(guī)則使用教程的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • element-ui form表單的動態(tài)rules校驗

    element-ui form表單的動態(tài)rules校驗

    在vue 項目中,有時候可能會用到element-ui form表單的動態(tài)rules校驗,比如說選擇了哪個選項,然后動態(tài)顯示或者禁用等等。 我們可以巧妙的運(yùn)用element-ui form表單里面form-item想的校驗規(guī)則來處理(每一個form-item項都可以單獨校驗)。 上代碼: 重點是這個: :rules=“sqyxForm.jtpslx

    2024年02月15日
    瀏覽(23)
  • 【element-ui】form表單動態(tài)修改rules校驗項

    【element-ui】form表單動態(tài)修改rules校驗項

    在項目開發(fā)過程中,該頁面有暫存和提交兩個按鈕,其中暫存和提交必填項校驗不一樣,此時需要動態(tài)增減必填項校驗 ,解決方法如下: 增加rules校驗項 刪除rules校驗項

    2024年02月04日
    瀏覽(34)
  • element UI —— form表單中Radio單選框進(jìn)行切換 & 表單驗證rule動態(tài)校驗-validator & 保存前進(jìn)行form表單校驗后才能上傳-validate

    element UI —— form表單中Radio單選框進(jìn)行切換 & 表單驗證rule動態(tài)校驗-validator & 保存前進(jìn)行form表單校驗后才能上傳-validate

    element UI —— form表單中Radio單選框進(jìn)行切換 表單驗證rule動態(tài)校驗-validator 保存前進(jìn)行form表單校驗后才能上傳-validate 1、效果圖 2、代碼 結(jié)構(gòu) 數(shù)據(jù)

    2024年02月07日
    瀏覽(34)
  • element-plus el-form 表單、表單驗證 使用方法、注意事項

    element-plus@2.0.6 及之后的版本,表單驗證不再是同步執(zhí)行的了 另外, element-plus@2.1.4 及之后的版本,才可按照官方文檔示例正常使用(使用的是兩者的中間版本的話,最好先自行確認(rèn)下正確的 上例中: 如果在“ 位置1 ”執(zhí)行表單驗證通過后的業(yè)務(wù)代碼,可以去掉 async...await 如

    2024年02月05日
    瀏覽(32)
  • Element-Puls Form表單內(nèi)嵌套el-table表格,根據(jù)表格復(fù)選框多選或單選動態(tài)設(shè)置行的驗證規(guī)則

    Element-Puls Form表單內(nèi)嵌套el-table表格,根據(jù)表格復(fù)選框多選或單選動態(tài)設(shè)置行的驗證規(guī)則

    根據(jù) Table 表格內(nèi)的復(fù)選框來控制當(dāng)前選中行是否添加必填校驗規(guī)則 我們需要設(shè)置一個 flag 來標(biāo)識已勾選的行,el-table渲染數(shù)據(jù)結(jié)構(gòu)是數(shù)組對象形式,我們可以在每個對象中手動加如一個標(biāo)識,例如默認(rèn):selected : false,如你的源數(shù)據(jù)中已有類似key,則可用它作于唯一標(biāo)識 htm

    2024年02月02日
    瀏覽(34)
  • Vue3+Element Plus 關(guān)于Dialog彈出Form表單,使用resetFields重置無效的解決

    主要參考了element-plus官方的表單重置按鈕(官方Form例子任意reset按鈕),然后試了試他的ref綁定,發(fā)現(xiàn)可以完美解決重置問題。 第一步:給Form表單綁定ref。綁定ref?的值為refFormInstance();這里注意表單el-form-item必須有prop屬性。 2.第二步:在你想要重置的地方調(diào)用重置表單方法

    2024年01月21日
    瀏覽(23)
  • el-form單個表單項校驗方法;element-ui表單單個選項校驗;el-form單個表單校驗

    el-form單個表單項校驗方法;element-ui表單單個選項校驗;el-form單個表單校驗

    當(dāng)我們使用element-ui的el-form時,想在提交表單前對其中一個表單進(jìn)行驗證時就可以使用element自帶的方法“validateField” 如圖: 使用示例

    2024年02月16日
    瀏覽(33)
  • element-ui的form表單校驗

    form表單校驗基本三步: 1、定義驗證規(guī)則 在data中定義表單校驗規(guī)則,一個表單項可定義多條規(guī)則,表單項規(guī)則用數(shù)組,規(guī)則為對象,required為必須填寫,message為校驗提示信息,trigger為校驗時機(jī),可選blur和change,分別為失去焦點和數(shù)據(jù)變化;min/max為最小與最大字符個數(shù),v

    2024年02月11日
    瀏覽(27)
  • vue3使用el-form實現(xiàn)登錄、注冊功能,且進(jìn)行表單驗證(Element Plus中的el-form)

    vue3使用el-form實現(xiàn)登錄、注冊功能,且進(jìn)行表單驗證(Element Plus中的el-form)

    簡介:Element Plus 中的 el-form 是一個表單組件,用于快速構(gòu)建表單并進(jìn)行數(shù)據(jù)校驗。它提供了豐富的表單元素和驗證規(guī)則,使表單開發(fā)變得更加簡單和高效??梢源钆鋏l-dialog實現(xiàn)當(dāng)前頁面的登錄、注冊頁 ,這兩天在vue3中用到了表單登錄,特意記錄一下,這里沒有封裝,直接使

    2024年02月07日
    瀏覽(130)
  • el-form表單中不同數(shù)據(jù)類型對應(yīng)的時間格式化和校驗規(guī)則

    el-form表單中不同數(shù)據(jù)類型對應(yīng)的時間格式化和校驗規(guī)則

    ?1. 在表單中, 當(dāng)選擇不同的數(shù)據(jù)類型時, 需要在下面選擇時間時和數(shù)據(jù)類型對應(yīng)上, 通過監(jiān)聽數(shù)據(jù)類型的變化, 給時間做格式化, 2. 但是當(dāng)不按順序選擇數(shù)據(jù)類型后, 再選時間可能會報錯, 所以需要在dom更新后, 再清空表單. 3. 校驗規(guī)則, 結(jié)束時間需要大于開始時間, 但是不能選當(dāng)

    2024年02月09日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包