Skip to content
Advertisement

How validate an array with a dot in key name ? (Laravel)

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”.

Advertisement

Answer

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']
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement