1

Here is to validate form request in laravel, request contains filter and field name in the filter has period(dot) present.

Sample Request url

...?filter[entity.abc][]='value'

Here entity.abc is actually a string, but laravel considers it to be array of object, when rule is given for 'filter.entity.abc'

filter:[
    [entity]: [ {abc:'value'}]
      ]

which is actually

filter:[
   [entity.abc]:['value']
]

So we need to make regex for second dot, which equivalents to:

public function rules()
{
    return [
        'filter.entity\.abc' => ['bail', 'sometimes', 'array'],
        'filter.entity\.abc' => ['uuid']
    ];
}

Above always retuns true,even when invalid uuid is present

Sonia Behal
  • 63
  • 1
  • 1
  • 5
  • 1
    Can you use an `_` or `-` instead of a `.`? Laravel uses "dot-notation" for a bunch of things, like nested form fields for Validation, etc etc. While the `.` may be valid, it goes against the framework you're using. Also, you can't have an array with the same key twice, it would need to be `filter.entity\.abc' => ['bail', 'sometimes', 'array', 'uuid']` – Tim Lewis Jan 05 '21 at 19:58
  • Thanks @TimLewis for highlighting this, it works. return [ 'filter.entity\.abc' => ['bail', 'sometimes', 'array'], 'filter.entity\.abc.*' => ['uuid'] ]; – Sonia Behal Jan 06 '21 at 06:40

1 Answers1

0

why not modify your request like this?

...?filter[entity][abc][]='value'

Edit:

You can use custom validation in laravel where you can deconstruct the parameters and check the values manually

Laravel Custom Validation https://laravel.com/docs/8.x/validation

Kevin Loquencio
  • 391
  • 1
  • 4
  • 16