a-form和a-form-model的区别和使用

1.a-form的使用

组件使用:

<a-form :model="bhform" :form="bhform">

        <a-form-item label="驳回原因" style="display: flex">

          <a-input  v-decorator="['refuseReason', { rules: [{ required: true, max: 20, message: '长度为1-20' }]}]" />

        </a-form-item>

     </a-form>

data定义:

    data() {

     return {
            bhform: this.$form.createForm(this, {name: 'coordinated'}),
     }
}

赋值:

 this.bhform.setFieldsValue({

        'refuseReason': '原因惺惺惜惺惺',

 })

清除值:

 this.bhform.setFieldsValue({

        'refuseReason': '',

 })

获取表单内容:

 this.bhform.getFieldValue('refuseReason')

重置:

 this.bhform.resetFields()

总体校验(提交):

this.bhform.validateFields((err, values) => {

        if (!err) { 

 //成功

        }

})



2.a-form-model的使用

组件使用:

 <a-form-model ref="form" :model="model" :rules="validatorRules" >

         <a-form-model-item label="通知内容" prop="noticeContent">

             <a-input v-model="model.noticeContent" ></a-input>

          </a-form-model-item>

  </a-form-model>  

data定义:

  data() {

       const integerValid = (rule, cellValue, callback) => {

            if (cellValue && !/^\w+$/.test(cellValue)) {

                return new Error('应为正整数')

            } else {

                callback()

            }

        } 

return {
    model:{},

  validatorRules: {

        noticeContent: [

            { max: 500, message: '超过最大长度500!'},

                    { validator: integerValid } //自定义校验

                ]
  }
}
 

赋值:

this.model.noticeContent='xxxxxx' 或者

this.$set(this.model,'noticeContent','xxxxxx')

清除值:

this.model.noticeContent='' 或者

this.$set(this.model,'noticeContent','')

获取表单内容:

 this.model.noticeContent

重置:

 this.$refs.form.resetFields()

总体校验(提交):

 this.$refs.form.validate(valid => {

            if (valid) {

//校验成功
}

})

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`a-form-item` 和 `a-form-model` 都是 Ant Design Vue 组件库中的表单组件,用于处理表单数据。它们的作用有所不同: - `a-form-item`:用于包装表单项,可以设置表单项的 label 和校验规则等属性。 - `a-form-model`:用于绑定表单数据,将表单数据与组件的数据模型进行双向绑定。 使用 `a-form-item` 包装表单项可以方便地设置表单项的属性,如下示例: ```html <template> <a-form> <a-form-item label="用户名" :rules="[{ required: true, message: '请输入用户名' }]"> <a-input v-model="username" /> </a-form-item> <a-form-item label="密码" :rules="[{ required: true, message: '请输入密码' }]"> <a-input type="password" v-model="password" /> </a-form-item> <a-form-item> <a-button type="primary" @click="submit">提交</a-button> </a-form-item> </a-form> </template> <script> export default { data() { return { username: '', password: '', }; }, methods: { submit() { // 提交表单数据 }, }, }; </script> ``` 使用 `a-form-model` 绑定表单数据可以方便地处理表单数据的变化,如下示例: ```html <template> <a-form :model="form" :rules="rules" ref="form"> <a-form-item label="用户名" prop="username"> <a-input v-model="form.username" /> </a-form-item> <a-form-item label="密码" prop="password"> <a-input type="password" v-model="form.password" /> </a-form-item> <a-form-item> <a-button type="primary" @click="submit">提交</a-button> </a-form-item> </a-form> </template> <script> export default { data() { return { form: { username: '', password: '', }, rules: { username: [{ required: true, message: '请输入用户名' }], password: [{ required: true, message: '请输入密码' }], }, }; }, methods: { submit() { this.$refs.form.validate((valid) => { if (valid) { // 提交表单数据 } else { console.log('error submit!!'); return false; } }); }, }, }; </script> ``` 在以上示例中,`a-form-model` 绑定了表单的数据模型 `form` 和校验规则 `rules`,在提交表单时通过 `validate` 方法对表单进行校验,最后提交表单数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值