3 years ago

Parse array validation from laravel

Posted 3 years ago by DimitriC

I have a form with one or more occurrences of the same info (name, role, email). One or more because the user can add more rows to insert (added to the DOM by vue v-for).

I have a data property that contains an array of the info

        members: [{name: '', surname: '', role: '', email:''}],
        submitted: false,
        errors: []

I post to the server via vue-resource post. If validation is wrong, I fetch the 422 return and save them in errors. I would now like the add the bootstrap has-error class to those input boxes that are incorrect. Unfortunate the json returns the following when looping through each error.

members.0.email: The members.0.email field is required.
members.1.email: The members.1.email field is required.
members.1.name: The members.1.name field is required.

What is the best way to parse these errors so I can add the has-error class to the first and second email box and the second name box?

I watched the Vue 2 series about forms but that solution only works if there is only one occurrence of a field.

Please sign in or create an account to participate in this conversation.