1
votes

I am having a registration form that uses vue inside Laravel blade file. I'm using a validator for invalid inputs of user and I want to get the old value of the field but now I can't make it work since some of the fields are using vue. Here's my code:

regist.blade.php

<div class="regist_input">
      <input type="email" name="email" v-model="email" v-bind:placeholder="placeholder_email" v-bind:class="{ error: emailError }">
      <p class="error_text" v-bind:class="{ active: emailError2 }">Invalid email.</p>
      @if($errors->has('email'))
          <p class="alert-error">
          {{ $errors->first('email') }}
          </p>
      @endif
</div>
<dd>
    <div class="regist_input">
        <input class="input_company" type="text" name="company" value="{{ old('company') }}" placeholder="company"> //this one works
    </div>
</dd>

controller

if($validator->fails()) {
    return redirect()->back()->withInput()->withErrors($validator->errors());
}

I can display its old value if out side the field, which means that my php function that validates is working.

I tried using v-bind:value="{{ old('email') }}" or :value="{{ old('email') }}" but is not working.For this one, how can I display the old value in the input field that has a vue component?

2

2 Answers

6
votes

You could pass the blade email data to vue using data attributes.

First should add an id to an HTML element with data attribute on it. Then use blade to parse your data and set the data attribute to email data.

<div id="email-data" data-email="{{ old('email') }}"></div>

TO BE EXACT (OP)

<input type="email"  value="{{ old('email') }}" name="email" v-model="email" id="email-data" data-email="{{ old('email') }}" v-bind:placeholder="placeholder_email"
    v-bind:class="{ error: emailError }">

Then in the created / mounted hook in vue extract the data and set to the email state.

created() {
    const el = document.getElementById('email-data')
    const email = el.dataset.email
    this.email = email
}
-1
votes

v-bind:value="{{ old('email') }}" or :value="{{ old('email') }} don't work because you are using v-model="email"

When you use v-model, it means that there should be an "email" property in the "data" attribute of a Vue component, and any value set to that "email" property will be what is displayed as the value of the input field.

See this Pen I created to illustrate both scenarios.

So, you'll need to remove the v-model if you want to use the v-bind:value. And now that I'm writing this, I don't think you need the v-bind:value and can just simply use value (just as you're doing with the company input.