Custom form Validation by Vuetify Showing Error _this.$refs[f].validate is not a function - vue.js

I want to make custom validation with Vuetify. My Vuetify version is 1.11.3. Here is my template. I set a ref to v-card according to this documentation.
<v-card ref="form">
<v-card-text>
<v-text-field
ref="name"
v-model="name"
label="Full Name:"
:rules="[() => !!name || 'Name is required']"
:error-messages="errorMessages"
required
light
>
</v-text-field>
<v-text-field
ref="email"
v-model="email"
label="Email Address:"
:rules="[
() => !!email || 'Email is required',
() => (!!email && /.+#.+/.test(email)) || 'Email must be valid',
]"
required
light
></v-text-field>
<VuePhoneNumberInput
ref="phone"
v-model="phone"
color="black"
dark-color="white"
size="lg"
default-country-code="BD"
light
required
/>
<v-textarea
ref="msg"
v-model="msg"
label="Message"
:rules="[() => !!msg || 'Message is required']"
light
></v-textarea>
</v-card-text>
<v-card-actions>
<v-btn #click="sendForm"> Submit </v-btn>
</v-card-actions>
</v-card>
I am trying to validate the form and textfields with their references.
This is my code:
data() {
return {
name: null,
email: null,
phone: null,
msg: null,
submitStatus: null,
formHasErrors: false,
errorMessages: '',
}
},
computed: {
form() {
return {
name: this.name,
email: this.email,
phone: this.phone,
msg: this.msg,
}
},
},
watch: {
name() {
this.errorMessages = ''
},
},
methods: {
sendForm() {
this.formHasErrors = false
Object.keys(this.form).forEach((f) => {
if (!this.form[f]) this.formHasErrors = true
this.$refs[f].validate(true)
})
}
When I submit the button, It shows
client.js?06a0:103 TypeError: _this.$refs[f].validate is not a
function
I get following error. What is the wrong with that?

I would recommence using V-form instead of v-card here.
Then you can check if your form is valid with the function this.$ref.myForm.validate() which returns a boolean
Here is a small example:
<v-card>
<v-card-text>
<v-form ref="myForm">
<v-row>
<v-col
cols="12"
sm="7"
>
<v-text-field
prepend-icon="mdi-tag-text"
v-model="form.name"
:rules="[
(v) => !!v || 'Name is requierd',
]"
label="Name"
/>
</v-col>
</v-row>
</v-form>
</v-card-text>
<v-card-actions>
<v-btn
#click="check"
>
Check
</v-btn>
</v-card-actions>
</v-card>
And the script :
export default {
data: () => ({
form:{
name : ""
},
}),
methods:{
check(){
if (this.$refs.myForm.validate()){
//form is valid
} else {
//form is not valid
}
}
}
}

Related

Capture the form inputs in the console

How do I capture the values of the form in the console. currently am getting an empty object in the console.
<div class="cont">
<v-form
ref="form"
class="form sign-in"
v-model="valid"
lazy-validation
>
<v-text-field
v-model="email"
:rules="emailRules"
label="E-mail"
outlined
required></v-text-field>
<v-text-field v-model="password"
:counter="10"
label="Password"
required
append-icon="mdi-map-marker"
></v-text-field>
<v-btn
:disabled="!valid"
color="success"
class="submit mr-8"
#click="submit" >Submit
</v-btn>
</v-form>
</div>
</template>
<script>
export default {
data: () => ({
form:{
name: '',
email: '',
password: ''
},
valid: true,
email: '',
emailRules: [
v => !!v || 'E-mail is required',
v => /.+#.+\..+/.test(v) || 'E-mail must be valid',
],
}),
methods: {
submit () {
console.log(this.form)
},
},
}
</script>
I want to see the data of the form when you click the submit button
I want to get the form details name email and password.
what i get now is
{__ob__: Observer}email: ""name: ""password: ""__ob__: Observerdep: Dep {_pending: false, id: 246, subs: Array(0)}mock: falseshallow: falsevalue: {__ob__: Observer}vmCount: 0[[Prototype]]: Objectget email: ƒ reactiveGetter()length: 0name: "reactiveGetter"prototype: {constructor: ƒ}arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
You should bind the form property fields to the inputs like v-model="form.name" and v-model="form.email" ..., and remove the other properties name,email and password :
<v-text-field
v-model="form.email"
:rules="emailRules"
label="E-mail"
outlined
required
></v-text-field>

How to programatically close a Veutify dialog

Hello everyone i was searching on the vuetify documentation a function or something like that to close a form dialog just after getting the axios response with the status 200 ..
i don't if there's a way to get a dialog instance and use a close() method on it like the bootstrap modals
here's my code :
template code
<template>
<v-dialog justify-center max-width="500px">
<template v-slot:activator="{on}">
<v-icon
small
v-on="on"
>
mdi-pencil
</v-icon>
</template>
<v-card>
<form #submit.prevent="submit">
<v-card-text>
<v-text-field
v-model="name"
label="Name"
required
></v-text-field>
<v-text-field
v-model="email"
label="E-mail"
required
></v-text-field>
<v-text-field
v-model="password"
label="password"
required>
</v-text-field>
</v-card-text>
<v-card-actions>
<v-btn
color="blue darken-1"
text
>close</v-btn>
<v-btn
color="blue darke-1"
text
type="submit"
>apply</v-btn>
</v-card-actions>
</form>
</v-card>
</v-dialog>
</template>
and here's the script
<script>
export default {
data () {
return {
name: '',
email: '',
password: ''
}
},
methods: {
submit() {
let Data = new FormData()
Data.append('name', this.name)
Data.append('email', this.email)
Data.append('password', this.password)
axios.post('http://stage.test/admin/users', Data)
.then(Response => {
if (Response.status === 200) {
}
})
}
},
}
</script>
Try to bind dialog component to a data property called open as follows :
<template>
<v-dialog v-model="open" justify-center max-width="500px">
<template v-slot:activator="{on}">
<v-icon
small
v-on="on"
>
mdi-pencil
</v-icon>
.....
then in then callback assign false to this.open
<script>
export default {
data () {
return {
open:false,
name: '',
email: '',
password: ''
}
},
methods: {
submit() {
let Data = new FormData()
Data.append('name', this.name)
Data.append('email', this.email)
Data.append('password', this.password)
axios.post('http://stage.test/admin/users', Data)
.then(Response => {
if (Response.status === 200) {
this.open=false
}
})
}
},
}
</script>

laravel vue with vuetify how to call this.loading = false;

im trying to add this.loading = false; in this below code.
.catch(
Toast.fire({
icon: 'warning',
title: 'Invalid Email or Password'
})
)
page code
<template>
<v-row justify="center">
<v-col cols="12" sm="6">
<form #submit.prevent="login">
<v-card ref="form">
<v-card-text>
<h3 class="text-center">Login</h3>
<v-divider class="mt-3"></v-divider>
<v-col cols="12">
<v-img
src="/assets/1.png"
class="my-3"
contain
height="50"
/>
</v-col>
<v-col cols="12" sm="12">
<v-text-field
v-model.trim="form.mobile_number"
type="number"
label="Mobile No"
solo
hide-details="auto"
></v-text-field>
<span class="form-text red--text" v-if="errors.mobile_number">
{{ errors.mobile_number[0] }}
</span>
<!-- <small class="form-text red--text" v-if="errors.mobile_number">{{errors.mobile_number[0]}}</small> -->
</v-col>
<v-col cols="12">
<v-text-field
v-model.trim="form.password"
type="password"
label="Password"
solo
hide-details="auto"
append-icon="mdi-eye"
></v-text-field>
<span class="form-text red--text" v-if="errors.password">
{{ errors.password[0] }}
</span>
<!-- <small class="form-text red--text" v-if="errors.password">{{errors.password[0]}}</small> -->
</v-col>
</v-card-text>
<v-divider class="mt-12"></v-divider>
<v-card-actions>
<v-spacer></v-spacer>
<div class="text-center">
<v-btn
rounded
type="submit"
:loading="loading"
color="primary"
dark
>Login</v-btn
>
</div>
</v-card-actions>
</v-card>
</form>
</v-col>
</v-row>
</template>
<script>
export default {
created(){
if (User.loggedIn()) {
this.$router.push({name: 'DashBoard'})
}
},
data(){
return {
loading: false,
form:{
mobile_number: null,
password: null
},
errors:[],
};
},
methods:{
login(){
this.loading = true;
axios.post('/api/auth/login',this.form)
.then(res => {
User.responseAfterLogin(res)
Toast.fire({
icon: 'success',
title: 'Signed in successfully'
})
this.$router.push({ name: 'DashBoard'});
})
.catch((error) => {this.loading = false;
{
this.errors = error.response.data.errors;
}
})
.catch(
Toast.fire({
icon: 'warning',
title: 'Invalid Email or Password'
})
)
.finally(() => {
this.loading = false
})
}
}
};
</script>
data(){
return {
loading: false,
form:{
mobile_number: null,
password: null
},
errors: {
mobile_number: null,
password: null,
},
};
},
axios.post('/api/auth/login',this.form)
.then(res => {
User.responseAfterLogin(res)
Toast.fire({
icon: 'success',
title: 'Signed in successfully'
})
this.$router.push({ name: 'DashBoard'});
})
.catch((error) => {
this.loading = false;
this.errors = error.response.data.errors;
Toast.fire({
icon: 'warning',
title: 'Invalid Email or Password'
})
})
.finally(() => {
this.loading = false
})

When saving an object using axios post, data from v-select does not persist in the Vuejs database, Vuetify datatable

I have a problem that when using the post method using axes I would need to send the data in the following json format:
{
"id":"1",
"firstName":"Faabio",
"lastName":"Mendes de Jesus",
"phone":"11941649284",
"mobilePhone":"11941649284",
"email":"art7design2013#gmail.com",
"gender":
{"name":"masculino"}
}
However, when saving, sex is stored only in the front, but it is not saved in the database, my console.log records the following situations: In object: config the data is correct and with the association to the gender, but in config: data the association no longer appears indicating that something is missing to persist the data and I'm going crazy about it, follow the console:
This line that was saved, in json format, gender is getting null and data table image with the gender field empty:
{
"id":"3",
"firstName":"ana",
"lastName":"lucia",
"phone":"1188888888",
"mobilePhone":"1188888888",
"email":"analu#gmail.com",
"gender":null
}
My file .vue
<template>
<v-data-table
:headers="headers"
:items="clients"
sort-by="firstName"
class="elevation-2"
>
<template v-slot:top>
<v-toolbar flat color="white">
<v-icon medium>mdi-account-supervisor</v-icon>
<v-toolbar-title> Clients</v-toolbar-title>
<v-divider
class="mx-4"
inset
vertical
></v-divider>
<v-spacer></v-spacer>
<v-dialog v-model="dialog" max-width="600px">
<template v-slot:activator="{ on }">
<v-btn
color="blue"
dark class="mt-6 mb-4"
v-on="on"
rounded
><v-icon medium>mdi-plus</v-icon>Add new</v-btn>
</template>
<v-card>
<v-card-title>
<span class="headline">{{ formTitle }}</span>
</v-card-title>
<v-card-text>
<v-container>
<v-form>
<v-row>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.firstName" label="First Name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.lastName" label="Last Name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.email" label="E-Mail"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.phone" label="Phone"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.mobilePhone" label="Mobile Phone"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<!-- select options-->
<v-select
label='Gender'
v-model='editedItem.gender'
:items='genders'
item-text='name'
return-object
>
</v-select>
</v-col>
</v-row>
</v-form>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="error" rounded #click="close">Cancel</v-btn>
<v-btn color="primary" rounded #click="save">Save</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
</template>
<template v-slot:item.action="{ item }">
<v-icon
small
color="green"
class="mr-2"
#click="editItem(item)"
>
mdi-pencil
</v-icon>
<v-icon
small
color="red"
#click="deleteItem(item)"
>
mdi-delete
</v-icon>
</template>
<template v-slot:no-data>
<v-btn color="primary" #click="initialize">Reset</v-btn>
</template>
</v-data-table>
</template>
<script>
import axios from 'axios'
import Client from '../../services/clients';
import Gender from '../../services/genders';
export default {
data: () => ({
dialog: false,
headers: [
{
text: 'First Name',
align: 'start',
sortable: false,
value: 'firstName',
},
{ text: 'Last Name', value: 'lastName' },
{ text: 'Email', value: 'email' },
{ text: 'Phone', value: 'phone' },
{ text: 'Mobile Phone', value: 'mobilePhone' },
{ text: 'Gender', value: 'gender.name' },
{ text: 'Actions', value: 'action', sortable: false },
],
clients: [],
genders: [],
errors: [],
editedIndex: -1,
editedItem: {
firstName: '',
lastName: '',
email: '',
phone: '',
mobilePhone: '',
gender: '',
},
defaultItem: {
firstName: '',
lastName: '',
email: '',
phone: '',
mobilePhone: '',
gender: '',
},
}),
computed: {
formTitle () {
return this.editedIndex === -1 ? 'New Item' : 'Edit Item'
},
},
watch: {
dialog (val) {
val || this.close()
},
},
created () {
this.initialize()
},
methods: {
initialize () {
Client.list().then(response => {
this.clients = response.data
}).catch(e => {
console.log(e)
});
Gender.list().then(response => {
this.genders = response.data
}).catch(e => {
console.log(e)
});
},
editItem (item) {
this.editedIndex = this.clients.indexOf(item)
this.editedItem = Object.assign({}, item)
this.editedID = this.editedItem.id
this.dialog = true
axios.put('http://192.168.26.130:3000/client/' + item.id)
.then(response => {
this.response = response
}).catch(error => {
console.log(error.response)
});
},
deleteItem (item) {
if (confirm("Do you really want to delete?")) {
const index = this.clients.indexOf(item)
this.deletedItem = Object.assign({}, item)
this.deletedID = this.deletedItem.id
this.clients.splice(index, 1);
axios.delete('http://192.168.26.130:3000/client/' + item.id)
.then(response => {
this.response = response
}).catch(error => {
console.log(error.response)
});
}
},
close () {
this.dialog = false
setTimeout(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
}, 300)
},
save () {
if (this.editedIndex > -1) {
Object.assign(this.clients[this.editedIndex], this.editedItem)
} else {
this.clients.push(this.editedItem)
axios.post('http://192.168.26.130:3000/client/', this.editedItem)
.then(response => {
console.log(response)
}).catch(error => {
console.log(error.response)
});
}
this.close()
},
},
}
</script>
The code snippet that saves to the database:
this.clients.push(this.editedItem)
axios.post('http://192.168.26.130:3000/client/', this.editedItem)
.then(response => {
console.log(response)
}).catch(error => {
console.log(error.response)
Could someone save me? Thank you very much in advance .

Problem in POST and PUT request, with axios, vuetify datatable, vuejs

I'm trying to adapt a sample datatable from the vuetify website itself according to my needs by implementing axios to consume my api. The GET AND DELETE method is working perfectly, however I am very confused about the POST AND PUT method, I am using 2 models as a client and the relationship with the genre, follows part of the code:
<template>
<v-data-table
:headers="headers"
:items="clients"
sort-by="firstName"
class="elevation-2"
>
<template v-slot:top>
<v-toolbar flat color="white">
<v-icon medium>mdi-account-supervisor</v-icon>
<v-toolbar-title> Clients</v-toolbar-title>
<v-divider
class="mx-4"
inset
vertical
></v-divider>
<v-spacer></v-spacer>
<v-dialog v-model="dialog" max-width="600px">
<template v-slot:activator="{ on }">
<v-btn
color="blue"
dark class="mt-6 mb-4"
v-on="on"
rounded
><v-icon medium>mdi-plus</v-icon>Add new</v-btn>
</template>
<v-card>
<v-card-title>
<span class="headline">{{ formTitle }}</span>
</v-card-title>
<v-card-text>
<v-container>
<v-form>
<v-row>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.firstName" label="First Name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.lastName" label="Last Name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.email" label="E-Mail"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.phone" label="Phone"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.mobilePhone" label="Mobile Phone"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<!-- select options-->
<v-select
label='Gender'
v-model='editedItem.gender.name'
:items='genders'
item-value='name'
item-text='name'
>
</v-select>
</v-col>
</v-row>
</v-form>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="error" rounded #click="close">Cancel</v-btn>
<v-btn color="primary" rounded #click="save">Save</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
</template>
<template v-slot:item.action="{ item }">
<v-icon
small
color="green"
class="mr-2"
#click="editItem(item)"
>
mdi-pencil
</v-icon>
<v-icon
small
color="red"
#click="deleteItem(item)"
>
mdi-delete
</v-icon>
</template>
<template v-slot:no-data>
<v-btn color="primary" #click="initialize">Reset</v-btn>
</template>
</v-data-table>
</template>
<script>
import axios from 'axios'
import Client from '../../services/clients';
import Gender from '../../services/genders';
export default {
data: () => ({
dialog: false,
headers: [
{
text: 'First Name',
align: 'start',
sortable: false,
value: 'firstName',
},
{ text: 'Last Name', value: 'lastName' },
{ text: 'Email', value: 'email' },
{ text: 'Phone', value: 'phone' },
{ text: 'Mobile Phone', value: 'mobilePhone' },
{ text: 'Gender', value: 'gender.name' },
{ text: 'Actions', value: 'action', sortable: false },
],
clients: [],
genders: [],
errors: [],
editedIndex: -1,
editedItem: {
firstName: '',
lastName: '',
email: '',
phone: '',
mobilePhone: '',
gender: '',
},
defaultItem: {
firstName: '',
lastName: '',
email: '',
phone: '',
mobilePhone: '',
gender: '',
},
}),
computed: {
formTitle () {
return this.editedIndex === -1 ? 'New Item' : 'Edit Item'
},
},
watch: {
dialog (val) {
val || this.close()
},
},
created () {
this.initialize()
},
methods: {
initialize () {
Client.list().then(response => {
this.clients = response.data
}).catch(e => {
console.log(e)
});
Gender.list().then(response => {
this.genders = response.data
}).catch(e => {
console.log(e)
});
},
editItem (item) {
axios.put('http://192.168.26.130:3000/client/' + item.id)
.then(response => {
this.editedIndex = this.clients.indexOf(item)
this.editedItem = Object.assign({}, item)
this.editedID = this.editedItem.id
this.dialog = true
this.response = response
}).catch(e => {
console.log(e)
});
},
deleteItem (item) {
if (confirm("Do you really want to delete?")) {
axios.delete('http://192.168.26.130:3000/client/' + item.id)
.then(response => {
const index = this.clients.indexOf(item)
this.deletedItem = Object.assign({}, item)
this.deletedID = this.deletedItem.id
this.clients.splice(index, 1);
this.response = response
}).catch(e => {
console.log(e)
});
}
},
close () {
this.dialog = false
setTimeout(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
}, 300)
},
save () {
if (this.editedIndex > -1) {
axios.post('http://192.168.26.130:3000/client/')
.then(response => {
Object.assign(this.clients[this.editedIndex], this.editedItem)
this.response = response.data
}).catch(e => {
console.log(e)
});
} else {
this.clients.push(this.editedItem)
}
this.close()
},
},
}
</script>
When opening the modal to add item, only when opening the select and modifying the genre this error already appears before even saving, as shown in the image:
When clicking on save it is saved only on the front, and when updating the page the record disappears, could someone give me a light?
Update Edit.
After some changes, I think I am closer to the solution but I came to the following obstacle, When saving the client item, the gender is stored empty.
of console.log and the item saved in the front end but in the database the gender is empty
The file DataTable.vue:
<template>
<v-data-table
:headers="headers"
:items="clients"
sort-by="firstName"
class="elevation-2"
>
<template v-slot:top>
<v-toolbar flat color="white">
<v-icon medium>mdi-account-supervisor</v-icon>
<v-toolbar-title> Clients</v-toolbar-title>
<v-divider
class="mx-4"
inset
vertical
></v-divider>
<v-spacer></v-spacer>
<v-dialog v-model="dialog" max-width="600px">
<template v-slot:activator="{ on }">
<v-btn
color="blue"
dark class="mt-6 mb-4"
v-on="on"
rounded
><v-icon medium>mdi-plus</v-icon>Add new</v-btn>
</template>
<v-card>
<v-card-title>
<span class="headline">{{ formTitle }}</span>
</v-card-title>
<v-card-text>
<v-container>
<v-form>
<v-row>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.firstName" label="First Name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.lastName" label="Last Name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.email" label="E-Mail"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.phone" label="Phone"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.mobilePhone" label="Mobile Phone"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<!-- select options-->
<v-select
label='Gender'
v-model='editedItem.gender'
:items='genders'
item-value='name'
item-text='name'
>
</v-select>
</v-col>
</v-row>
</v-form>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="error" rounded #click="close">Cancel</v-btn>
<v-btn color="primary" rounded #click="save">Save</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
</template>
<template v-slot:item.action="{ item }">
<v-icon
small
color="green"
class="mr-2"
#click="editItem(item)"
>
mdi-pencil
</v-icon>
<v-icon
small
color="red"
#click="deleteItem(item)"
>
mdi-delete
</v-icon>
</template>
<template v-slot:no-data>
<v-btn color="primary" #click="initialize">Reset</v-btn>
</template>
</v-data-table>
</template>
<script>
import axios from 'axios'
import Client from '../../services/clients';
import Gender from '../../services/genders';
export default {
data: () => ({
dialog: false,
headers: [
{
text: 'First Name',
align: 'start',
sortable: false,
value: 'firstName',
},
{ text: 'Last Name', value: 'lastName' },
{ text: 'Email', value: 'email' },
{ text: 'Phone', value: 'phone' },
{ text: 'Mobile Phone', value: 'mobilePhone' },
{ text: 'Gender', value: 'gender.name' },
{ text: 'Actions', value: 'action', sortable: false },
],
clients: [],
genders: [],
errors: [],
editedIndex: -1,
editedItem: {
firstName: '',
lastName: '',
email: '',
phone: '',
mobilePhone: '',
gender: '',
},
defaultItem: {
firstName: '',
lastName: '',
email: '',
phone: '',
mobilePhone: '',
gender: '',
},
}),
computed: {
formTitle () {
return this.editedIndex === -1 ? 'New Item' : 'Edit Item'
},
},
watch: {
dialog (val) {
val || this.close()
},
},
created () {
this.initialize()
},
methods: {
initialize () {
Client.list().then(response => {
this.clients = response.data
}).catch(e => {
console.log(e)
});
Gender.list().then(response => {
this.genders = response.data
}).catch(e => {
console.log(e)
});
},
editItem (item) {
axios.put('http://192.168.26.130:3000/client/' + item.id)
.then(response => {
this.editedIndex = this.clients.indexOf(item)
this.editedItem = Object.assign({}, item)
this.editedID = this.editedItem.id
this.dialog = true
this.response = response
}).catch(error => {
console.log(error.response)
});
},
deleteItem (item) {
if (confirm("Do you really want to delete?")) {
axios.delete('http://192.168.26.130:3000/client/' + item.id)
.then(response => {
const index = this.clients.indexOf(item)
this.deletedItem = Object.assign({}, item)
this.deletedID = this.deletedItem.id
this.clients.splice(index, 1);
this.response = response
}).catch(error => {
console.log(error.response)
});
}
},
close () {
this.dialog = false
setTimeout(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
}, 300)
},
save () {
if (this.editedIndex > -1) {
Object.assign(this.clients[this.editedIndex], this.editedItem)
} else {
this.clients.push(this.editedItem)
axios.post('http://192.168.26.130:3000/client/', this.editedItem)
.then(response => {
console.log(response)
}).catch(error => {
console.log(error.response)
});
}
this.close()
},
},
}
</script>
Please would someone help me?
There are a couple of issues. First, you aren't passing any data to your PUT and POST requests. They should look something like:
editItem (item) {
// YOU NEED TO PASS AN OBJECT TO THE PUT REQUEST ▼▼HERE▼▼
axios.put('http://192.168.26.130:3000/client/' + item.id , item)
.then(response => {
// handle response...
})
.catch(err => { console.log(error) })
},
save () {
if (this.editedIndex > -1) {
// YOU NEED TO PASS AN OBJECT TO THE POST REQUEST ▼▼HERE▼▼
axios.post('http://192.168.26.130:3000/client/', this.editedItem)
.then(response => {
// handle response...
})
.catch(err => { console.log(error) })
} else { /* ... */ }
},
Second, under the hood, <v-select> uses a v-for to iterate over all of the options that are supposed to go into the dropdown menu. If this were a plain HTML <select> element, it would look something like this:
<select name="gender">
<option value="">Select a gender...</option>
<option
v-for="gender in genders"
:key="gender"
value="gender.value"
>
{{ gender.text }}
</option>
</select>
Vuetify expects the array of genders to be in one of two formats, either an array of strings, or an array of objects with text and value properties:
const genders = ['male', 'female', 'other']
// OR
const genders = [
{ value: 1, text: 'male' }, // `value` can be anything you want
{ value: 2, text: 'female' },
{ value: 3, text: 'other' },
]
Alternatively, if your genders array has a different data structure, you can tell Vuetify what properties to use for the value and text properties (this is what it looks like you did). So if your genders array looks like this:
const genders = [
{ name: 'male' },
{ name: 'female' },
{ name: 'other' },
]
Your <v-select> should look like this (in your case you used the SAME property for both text and value, which is perfectly fine to do):
<v-select
v-model="editedItem.gender"
:items="genders"
item-text="name"
return-object
/>
What I'm guessing, based on the image you attached, is that the genders array is NOT in one of these formats, and this is causing an error when Vuetify tries to turn it into a dropdown. Also, I think you intend for the value selected to be assigned to editedItem.gender and not editedItem.gender.name. Here's a codepen showing how to use objects for v-select items.
If the items array is in one of the two formats I showed before, you do NOT need to specify the item-text and item-value props. They will be detected automatically.
Hope this helps!