0

I'm was using server side validation with laravel and fecthing the old value in case there was a validation error.

<input type="text" class="form-control" name="nombre" value="{{ old('nombre')}}">

But now I need to validate that input on the client with vue, cause it's required for other stuff.

<input type="text" class="form-control" name="nombre" value="{{ old('nombre')}}" v-model="vNombre" v-on:keyup="validarNombre">

The problem is that when the validation fails on the server side the old input value get's replaced with an empty string cause Vue is initializing that variable. Please help, thanks for reading.

<script type="text/javascript">
  var avatar = new Vue({
    el: '#validaciones',
    data: {
      vNombre: '',
      validNombre: false,
    },
    methods: {
      validarNombre: function(){
        if (this.vNombre == '') {
          this.validNombre = false
        }
        else {
          this.validNombre = true;
        }
      }
    }
  })
</script>

1 Answer 1

1

Since vue is overwriting the field data you can use {{ old('nombre')}} in vNombre vue data field

data: {
   vNombre: '{{ old("nombre")}}', //Be careful with single and double quotes
   validNombre: false,
},
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This is what I was using: $('input[name=nombre]').val(), it worked fine also, but your solution is better

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.