我正在使用此示例来提交表单https://quasar.dev/vue-components/form 当我将按钮div放置在q-form之外时,提交和验证不起作用
<div>
<q-btn label="Submit" type="submit" color="primary"/>
<q-btn label="Reset" type="reset" color="primary" flat class="q-ml-sm" />
</div>
有什么帮助吗?
答案 0 :(得分:0)
您可以使用ref
,然后调用validate()
方法进行验证。
<div id="q-app">
<div class="q-pa-md" style="max-width: 400px">
<q-form
ref="myForm"
class="q-gutter-md"
>
<q-input
filled
v-model="name"
label="Your name *"
hint="Name and surname"
lazy-rules
:rules="[ val => val && val.length > 0 || 'Please type something']"
></q-input>
<q-input
filled
type="number"
v-model="age"
label="Your age *"
lazy-rules
:rules="[
val => val !== null && val !== '' || 'Please type your age',
val => val > 0 && val < 100 || 'Please type a real age'
]"
></q-input>
<q-toggle v-model="accept" label="I accept the license and terms" ></q-toggle>
</q-form>
<div>
<q-btn @click="onSubmit" color="primary">Submit</q-btn>
<q-btn @click="onReset" flat class="q-ml-sm" >Reset</q-btn>
</div>
</div>
</div>
methods: {
onSubmit () {
let vm = this;
this.$refs.myForm.validate()
.then(success => {
vm.$q.notify({
color: 'red-5',
textColor: 'white',
icon: 'warning',
message: 'You need to accept the license and terms first'
})
})
.catch(err => {
vm.$q.notify({
color: 'green-4',
textColor: 'white',
icon: 'cloud_done',
message: 'Submitted'
})
})
},
onReset () {
this.name = null
this.age = null
this.accept = false
}
}
使用this.$refs.myForm.resetValidation();
进行重置验证。
工作版本-https://codepen.io/Pratik__007/pen/ExajNEq?editors=1010