I am using Vue-multiselect with Laravel.
I am using the multiselect component in my form to let the user select multiple countries. The component works fine but when I submit the form and I dd()
it, it shows [object Object]
.
I can’t get the value of the multiselect component. I have found similar questions but none of them worked for me.
Here is my code:
The ExampleComponent.vue file:
JavaScript
x
64
64
1
<template slot-scope="{ option }">
2
<div>
3
4
<label class="typo__label">Restricted country</label>
5
<multiselect
6
v-model="internalValue"
7
tag-placeholder="Add restricted country"
8
placeholder="Search or add a country"
9
label="name"
10
name="selectedcountries[]"
11
:options="options"
12
:multiple="true"
13
track-by="name"
14
:taggable="true"
15
@tag="addTag"
16
>
17
</multiselect>
18
19
<pre class="language-json"><code>{{ internalValue }}</code></pre>
20
21
</div>
22
</template>
23
24
<script>
25
import Multiselect from 'vue-multiselect'
26
27
// register globally
28
Vue.component('multiselect', Multiselect)
29
30
export default {
31
32
components: {
33
Multiselect
34
},
35
props: ['value'],
36
data () {
37
return {
38
internalValue: this.value,
39
options: [
40
{ name: 'Hungary' },
41
{ name: 'USA' },
42
{ name: 'China' }
43
]
44
}
45
},
46
watch: {
47
internalValue(v){
48
this.$emit('input', v);
49
}
50
},
51
methods: {
52
addTag (newTag) {
53
const tag = {
54
name: newTag,
55
code: newTag.substring(0, 2) + Math.floor((Math.random() * 10000000))
56
}
57
this.options.push(tag)
58
this.value.push(tag)
59
}
60
},
61
62
}
63
</script>
64
Here is my register form:
JavaScript
1
14
14
1
<div id="select">
2
<example-component v-model="selectedValue"></example-component>
3
<input type="hidden" name="countriespost" :value="selectedValue">
4
</div>
5
6
<script>
7
const select = new Vue({
8
el: '#select',
9
data: {
10
selectedValue: null
11
},
12
});
13
</script>
14
When I submit the form, the countriespost
shows me me this: [object Object]
instead of the actual value.
Advertisement
Answer
It’s because you are providing an array of objects as options
property:
JavaScript
1
6
1
options: [
2
{ name: 'Hungary' },
3
{ name: 'USA' },
4
{ name: 'China' }
5
]
6
so the value emited on input
is an object.
Try to change the options to following:
JavaScript
1
2
1
options: [ 'Hungary', 'USA', 'China' ]
2