Laravel validate request filter field with dot (period) in field name

1.4k Views Asked by At

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

1

There are 1 best solutions below

1
Kevin Loquencio On

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