#Array validation

8 messages · Page 1 of 1 (latest)

static frost
#

Hi, I'm trying to validate an array but I don't know if I'm overcomplicating it.

I'm using Inertiajs with Vue, and I'm trying to pass my errors into a component just using :errors="form.errors.ticket[index]" but I can't seem to figure out a way to structure the errors returned from the validator in a way that would make it easy to pass to the component.

By default Laravel will return multidimensional array errors like:

tickets.0.description:"The ticket description is required."
tickets.0.price:"The tickets.0.price field must have 0-2 decimal places."
tickets.1.description:"The ticket description is required."
tickets.1.name:"The ticket name is required."
tickets.1.price:"The tickets.1.price field must have 0-2 decimal places."

is there any way to create a structure like:

tickets: {
  0: {
    description: "The ticket description is required.",
    price:"The tickets.0.price field must have 0-2 decimal places."
  },
  1: {
    description: "The ticket description is required.",
    price:"The tickets.1.price field must have 0-2 decimal places."
  }
  ...etc
}
rotund canyon
#

Not by default i don't think

#

you could use a collection to manipulate it into the format you want though

#

there might even be a helper function, one second...

#

This should do it for you

static frost
#

@rotund canyon
Awesome! That looks like it'll do it.

Seems like a bit of a weird way for Laravel to return the errors tbh, would make much more sense if it was all in a key => value object to make it easier to loop through, oh well.

Thanks!

rotund canyon
#

I think this way, it's a "flat" structure that you can run through with one foreach statement to display the errors.