I am trying to write a form validation using VueJS.
I keep testing for the length of the error object. I keep getting undefined when I log it console. I used this.errors.length to refer to it. It seems to treat .length as a key in errors.
data(){
return {
name: null,
price: null,
description: null,
roomTypes: {},
errors: {},
}
},
methods: {
addRoomType: function(){
if(this.name && this.description && this.price && this.this.description>10){
axios.post('/admin/room-types',{
name: this.name,
price: this.price,
description: this.description
}).then((response)=>{
this.errors = {};
this.roomTypes.push(response.data);
}).catch((error)=>{
this.errors = error.response.data;
console.error(error.response.data);
});
}
//this.errors = {};
if(!this.name){
this.errors.name = "Please enter a name.";
console.log(this.errors.name);
}
if(!this.description){
this.errors.description = "Please enter a description.";
console.log(this.errors.description);
}
if(!this.price){
this.errors.price = "Please enter a price.";
console.log(this.errors.price);
}
if(this.errors.length){
console.log(this.errors.length);};
I want to be able to get the size of the errors object so i can check if it is empty.