Home > Back-end >  How validate an array with a dot in key name ? (Laravel)
How validate an array with a dot in key name ? (Laravel)

Time:05-19

How can I validate this array in laravel validation:

"price" => array:1 [▼
           0 => array:1 [▼
                "price.value" => 50
]

I tried to use this, but it does't working:

'price.0.price.value' => ['required']

How can I solve? I want to note that: I have no choice to change the name "price.value".

CodePudding user response:

As you cannot change the name price.value you have to escape the dot since in Laravel array validation a dot means a nested level. Try to escape the. with \
Your data:

 "price" => array:1 [▼
           0 => array:1 [▼
                "price.value" => 50
]

Validation rule with dot escaped:

'price.0.price\.value' => ['required']

CodePudding user response:

Example:

{
  filters: [{field: '', value: ''}]
}

     
'filters' => 'nullable|array',
'filters.*.field' => 'required|max:60',
'filters.*.value' => 'nullable|max:60',
'filters.*.cond' => 'nullable|max:10',

CodePudding user response:

try it

'price.*.value' => 'required'
  • Related