1

2 weeks ago I found out i couldn't solve this and went to google for a solution which dind't really give me one, maybe the wrong type of googling not sure. Let me get to the point.

I got this piece of html and vue which makes dynamically a extra phone field when a button is pressed. the thing is when a Laravel validation fails i do not know how to get the old() data into Vue, so I can push that to the phone_number_field array.

<a v-on:click="addPhoneNumberField">
    <span class="panel-title glyphicon glyphicon-plus" aria-hidden="true"></span>
</a>
<a v-on:click="deletePhoneNumberField" v-if="phone_number_field.length > 0">
    <span class="panel-title glyphicon glyphicon-remove" aria-hidden="true"></span>
</a>

<div class="phone row" v-for="(field, index) in phone_number_field">
    <input id="phone_number_number" type="text" class="form-control" :name="'customer[phone_numbers][' + index + '][number]'" placeholder="Number" required>
    <input id="phone_number_description" type="text" class="form-control" :name="'customer[phone_numbers][' + index + '][description]'" placeholder="Description">  
</div>

Vue part

var bindUser = new Vue({
    el: '#bindUser',

    data: {
        phone_number_field: [],
    },

    methods: {
        addPhoneNumberField() {
            this.phone_number_field.push({
                number: '',
                description: ''
            })
        },

        deletePhoneNumberField(index) {
            this.phone_number_field.splice(index, 1)
        },
    },
});
3
  • Is your page getting refresh everytime ? Commented Feb 25, 2018 at 5:37
  • Why not using ajax to submit form ? Commented Feb 25, 2018 at 5:38
  • I'm not that familiar with JS in general and am more curious if this is possible with Vue Commented Feb 25, 2018 at 8:51

2 Answers 2

4

Old values are inside old('customer.phone_numbers'), so just pass it encoded into vue initiating.

Your blade template (somewhere before js include):

<script>
   var phoneValues = {!! json_encode(old('customer.phone_numbers', [])) !!};
</script>

Your js file:

data: {
    phone_number_field: phoneValues,
},

Where [] is the default value. But I think it's better to have all the initiating code in one place (blade template).

Sign up to request clarification or add additional context in comments.

3 Comments

This results into a syntax error phone_number_field: {!! json_encode(old('customer.phone_numbers', [])) !!}, I got the Vue instance in a seperate app.js
Well, you have to initiate vuejs app values in your blade template. The rest logic can be in the js file.
So simple, thanks. this has been holding me back for a while. Also most of the times i tried to declare it within my #bindUser div, but it has to be outside the div else Vue will throw a error
0

Here's an idea.. Why don't you do something along these lines:

let phone_number_field = [];
<?php if (!empty($_POST['phone_number_field']: ?>
    phone_number_field = []; //Loop foreach or whatever you need to build it
<?php endif; ?>

Then in VUE you can just initalize it with the prepouplated or empty phone_number_field you precreated

3 Comments

I tried setting values outside from the view instance (in blade template) Can't get it to work.
@Cruorzy did you assign this variable before vue is instanced?
I did it before my vue instance was loaded/created but within the div that the vue instance focussed on, and in there you can't/shouldn't use script tags. But it works now :)

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.