'why Vuetify rule take 0 as not set in lazy validation?
I'm asigning defaults values from API but when the value of v-text-field is 0 the :rules tag take this as not set: image
here is my code
<v-col cols="12" sm="6" md="4">
<v-text-field type="number" :rules="incapacidadRules" min="0" v-model="editedItem.incapacidad" label="Incapacidad"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field type="number" min="0" :value="editedItem.descanso" :rules="descansosRules" v-model="editedItem.descanso" label="Descansos"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="4">
<v-text-field type="number" min="0" :rules="permisosRules" v-model="editedItem.permiso" label="Permisos"></v-text-field>
</v-col>
and my rules
incapacidadRules: [
v => !!v || 'Incapacidad Requerido'
],
descansosRules: [
v => !!v || 'Descansos Requerido'
],
permisosRules: [
v => !!v || 'Permisos Requerido'
],
When I manually set the value to 0 then there is no problem with the rule.
Solution 1:[1]
0 is falsy. So !!0 == false. For your rules, maybe use typeof v === "number" ? v : 'Incapacidad Requerido' rather than !!v.
Solution 2:[2]
Consider !~a is true for a >= 0 and false for a < 0.
Solution 3:[3]
I could solve this: as Tanner mentions 0 is a falsy, So when you set the value as
editItem(item){
this.editedItem.faltas=item.faltas;
}
and the item.faltas is 0 the v-text-field is recognized as false, but with values differents to 0 there is no problem. So, you need to set always the values as string:
editItem(item){
this.editedItem.faltas=`${item.faltas}`;
}
Solution 4:[4]
You can use parseInt() or other variants of type conversion to validate on the numeric value and not the string value. v-text-field> represents string values and 0 is falsy hence the rule being triggered.
(v) => {
if (parseInt(v) >= 0) return true;
return 'Incapacidad Requerido';
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tanner |
| Solution 2 | Estradiaz |
| Solution 3 | Mickael Lherminez |
| Solution 4 | Gideon Van |
