我想验证一个可选的值“股票”。我正在使用 vuelidate ( https://monterrail.github.io/vuelidate/ )
如果其他值 ('price') 大于 0,则值 'stock' 必须大于 0
如果 'price' 值未定义,则必须禁用股票价值验证器
price 是输入值(十进制数),stock 是滑块(数字)
[编辑]
代码示例
<template>
<form @submit.prevent="submit" class="offer-form">
<q-field label="Prix initial" helper="Indiquer le prix initial de l'offre" :error="hasItemError('price')" :error-label="itemErrorLabel('price')">
<q-input type="number" color="input" @blur="$v.item.price.$touch()" v-model="item.price" />
</q-field>
<q-field label="Stock" helper="Indiquer le nombre de stock" :error="hasItemError('stock')" :error-label="itemErrorLabel('stock')">
<q-slider color="input" label-always label :step=1 :min="0" @blur="$v.item.stock.$touch()" :max="50" v-model="item.stock"/>
</q-field>
<q-btn
icon="fas fa-check"
label="Valider"
color="green"
@click="submit"
/>
</form>
</template>
<script>
import { required, requiredIf, decimal, minValue } from 'vuelidate/lib/validators'
export default {
data () {
return {
item: {
id: null,
title: '',
description: '',
price: null,
reductionPercentage: 15,
stock: 0,
startDate: new Date(),
endDate: null,
product: null,
shop: null,
images: []
},
},
validations: {
item: {
title: { required },
startDate: { required },
endDate: { required },
price: { decimal },
reductionPercentage: {
requiredIf: requiredIf((vueInstance) => {
return vueInstance.price > 0
}),
minValue: minValue(15)
},
stock: {
requiredIf: requiredIf((vueInstance) => {
return vueInstance.price > 0
}),
// minValue ???
}
}
},
methods: {
submit () {
this.onSubmit = true
if (this.isValidate()) {
// Validation ok
}
this.onSubmit = false
},
isValidate () {
this.$v.item.$touch()
if (this.$v.item.$error) {
this.$q.notify({message: 'Vérifiez les champs en erreur', position: 'center'})
return false
}
return true
}
}
}
我对此进行了测试,但不行=>
minValue: minValue((vueInstance) => {
return vueInstance.price > 0 ? vueInstance.stock > 0 : true
})
我怎样才能做到这一点 ?
谢谢