I'm using VUE and Vuelidate for validating form input. In this case.. It's a modal popup which gets data through ...mapGetters from the store.
If i set static values like this, it works :
validations: {
refundAmount: {
between: between(0, 40)
}
},
but when using a value from the store like this, it doesn't map against the value :
validations: {
refundAmount: {
between: between(0, this.selectedOrder ? this.selectedOrder.totalprice : 0)
}
},
My hard guess... this.selectedOrder.totalprice is not existing before the vuelidate component needs it ? Not sure how to actually handle that one...
The current validation (between) accepts only from 0 to 0 (zero to zero) so clearly... the value from this.selectedOrder.totalprice is not existant...
Here is my code :
import bus from '../global/bus.js'
import {
mapGetters
} from "vuex"
import {
required,
minLength,
between
} from 'vuelidate/lib/validators'
var moment = require('moment')
var alertify = require('../assets/alertify.js')
export default {
name: "modal",
data() {
return {
comment: null,
refundAmount: null,
paymentOptionsAgreementId: null,
moveToAppId: null
}
},
validations: {
refundAmount: {
between: between(0, this.selectedOrder ? this.selectedOrder.totalprice : 0)
}
},
mounted() {
this.$v.$touch()
},
computed: {
...mapGetters(["visibleModalComponent", "selectedOrder", "loggedInUser", "accessToken", "paymentoptions"])
}
}
<template>
<div class="form-group">
<label class="col-sm-5">Refundér beløp</label>
<div class="col-sm-7">
<input
v-model.trim="refundAmount"
v-on:input="$v.refundAmount.$touch"
v-bind:class="{error: $v.refundAmount.$error,
valid: $v.refundAmount.$dirty &&
!$v.refundAmount.$invalid}"
type="text"
class="form-control">
</input>
<pre>{{ $v }}</pre>
</div>
</div>
</template
Updated with computed value (sat correctly), but still not working :
Computed code :
computed: {
...mapGetters(["visibleModalComponent", "selectedOrder", "loggedInUser", "accessToken", "paymentoptions"]),
totalPrice() {
return this.selectedOrder ? this.selectedOrder.totalprice : 0
}
},
Validation :
validations: {
refundAmount: {
between: between(0, this.totalPrice)
}
},
Screenshot :
