4

problem with vue.js multiple select i tried many solution but i didn't find the solution and i get error [Vue warn]: expects an Array value for its binding, but got String

     <select name="users_id[]" multiple class="form-control" v-model="model.users_id" >
         <option>Select</option>
         <option v-for="users in option.users"
         v-bind:value="users.id">
         {{users.name}}
        </option>[![enter image description here][1]][1]
        </select>
 <script>   
    export default {
            props: ['title'],
            data(){
                return {
                   model: {
                        'title': '',
                        'users_id': '',
                    },
                    option: {
                        users: []
                    },
                }
            },
            created(){
                this.fetchData();
            },
            methods: {
                fetchData() {
                    let vm = this;
                    axios.get('/subject/create')
                        .then(function(response) {
                            Vue.set(vm.$data, 'option',   response.data.option)
                        })
                        .catch(function(error) {
                            console.log(error)
                        })
                },
</script>

enter image description here

0

3 Answers 3

13

One problem : multiple v-model="model.users_id" and users_id': '' :

You have a v-model with multiple options so the variable link with v-model must be an Array. But You passed a String with users_id': ''.

So do :

model: {
  'title': '',
  'users_id': [],
},
Sign up to request clarification or add additional context in comments.

1 Comment

What if you are generating the name of the link on the fly like: v-model="search[name_of_field]?
3

Change:

model: {
  'title': '',
  'users_id': '',
},

To:

model: {
  'title': '',
  'users_id': []
},

Comments

0

In this line:

Vue.set(vm.$data, 'option',   response.data.option)

you are setting the option property, which should be an object containing an Array named users. The value you are setting it to is (probably) a JSON-encoded form of that, which is to say, a string.

This might fix it:

Vue.set(vm.$data, 'option', JSON.parse(response.data.option))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.