2

validation from Laravel to VueJS.

I need to show the right error messages to the right input field. Any one have an idea?

form.vue :

<tr v-for="(item, index) in form.items">
 <td>
 <input type="text" v-model="item.name" class="form-control">
  <small class="text-danger" v-if="errors.name">
  {{errors.name[0]}}</small>
  </td>
   <input type="text" v-model="item.age" class="form-control">
  <small class="text-danger" v-if="errors.age">
  {{errors.age[0]}}</small>
  </td>
  </tr>

Controller :

$this->validate($request, [
        'items.*.name' => 'required',
        'items.*.age' => 'required',
    ]);

I got this in my vue devtools

enter image description here

3
  • And what's the problem ? Commented Apr 27, 2017 at 10:19
  • The error does not appear Commented Apr 27, 2017 at 10:21
  • it seems that errors is ne array and not name. {{errors[0]}} start with a dump of the error onutput and analyze the actual structure or read the offical docs for it? Commented Apr 27, 2017 at 10:37

3 Answers 3

1

It looks like you have to itterate through your errors object. Within error your key is "items.0.name", so probably you have to use {{ errors['items.0.name'] }}. But I think you would have to change your laravel error validation.

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

Comments

1

is an example of two dimensional arrays

validacion.revision[0].vehiculo_revision_all

right way

validacion['revision.'+index+'.vehiculo_revision_all']

validations in the controller

$rules =[
  'revision.*.vehiculo_revision_all.*.cantidad' => 'required',
  'revision.*.vehiculo_revision_all.*.estado' => 'required'
];

array output data

revision.2.vehiculo_revision_all.0.cantidad: ["El campo revision.2.vehiculo_revision_all.0.cantidad es obligatorio."]
revision.2.vehiculo_revision_all.0.estado: ["El campo revision.2.vehiculo_revision_all.0.estado es obligatorio."]
revision.2.vehiculo_revision_all.1.cantidad: ["El campo revision.2.vehiculo_revision_all.1.cantidad es obligatorio."]



<tr v-for="(revision, index) in form.items" v-bind:key="data.id">
     <tr v-for="(data, ins) in form.items" v-bind:key="data.id">
       <span v-if="validacion['revision.'+index+'.vehiculo_revision_all.'+ins+'.estado'] ">
                {{ validacion['revision.'+index+'.vehiculo_revision_all.'+ins+'.estado'] }}
        </span>
     </tr>
</tr>

Comments

0

I display my error messages using sweet alert. I catch it first on my axios post.

     createAdminstrator(name, username, password, confirmpassword, superadmin) {
        const t = this;

        axios.post('/administrators', {name: name, username: username, password: password, confirmpassword: confirmpassword, superadmin: superadmin})
            .then(({data}) => {
                t.administrators.unshift(data);
                t.error = '';
            })
            .catch(error => {
                if (error.response.data.errors.username) {
                  swal(error.response.data.errors.username[0], "Error", "error");
                } else if (error.response.data.errors.confirmpassword) {
                  swal(error.response.data.errors.confirmpassword[0], "Error", "error");
                } else if (error.response.data.errors.password) {
                  swal(error.response.data.errors.password[0], "Error", "error");
                }
              });
    }

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.