lundi 16 avril 2018

Laravel Validator removing null fields from array

I'm writing a test case to simulate a creation of a product. When creating the product, I'm passing an array of formats, and for some reasons, when validating with the controller, the thumbnail key is missing from the data.

This is the data I'm sending:

array:1 [
  0 => array:3 [
    "upc" => "584979099857"
    "vcode" => "VX43V14FN910479274209"
    "thumbnail" => null
  ]
]

And I send it like the following in my test case:

$response = $this->post(route('products.store'), $product);

When I do a dd(request()->all()); before the validation, I can see that the thumbnail is present. Therefore, I did receive the payload properly.

However, when validating the request, I always get the error that the thumbnail has to be present.

request()->validate([
    'formats' => 'bail|required|array',
    'formats.*.upc' => 'bail|required|string|max:255',
    'formats.*.vcode' => 'bail|required|string|max:255',
    'formats.*.thumbnail' => 'bail|present|image'
]);

I've also tried changing the rules of thumbnail to:

bail|required|nullable|image

Or

bail|nullable|required|image

But then I get the required error.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire