0

I have an array of objects with different, dynamic parameters. And I'm trying to find a way to validate it based on the object type value. Is there any way to make it with the FormRequest?

[
  {
    type: “link”, 
    url: “https://link.com”, 
    ...
  }, 
  {
    type: “image”, 
    id: “drre-ggre-765”, 
    image: “url”
    ...
  }
]
3
  • laravel.com/docs/8.x/validation#validating-nested-array-input Does this helps you ? Commented Oct 18, 2021 at 5:08
  • @PSA no, not really. It’s perfectly works when I know the structure of the object. But, in my case I need to choose a validation rules, for each object from the array, based on “type: value.” Commented Oct 18, 2021 at 8:11
  • perhaps you could loop over the array and build the validation based on the properties existing within - of course you could define these based on type like so: $this->{'validate' . ucwords($object->type)}($object); which would then call $this->validateImage($object) for example. Commented Oct 18, 2021 at 8:52

0

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.