I tried some validation rule like: Boolean and Required file with nested array field, but always failing
for example, I tried creating form request like this:
<?php
namespace AppHttpRequestsTest;
use IlluminateFoundationHttpFormRequest;
class Test extends FormRequest
{
public function validationData()
{
return [
'booleanField' => $this->boolean("booleanField"),
'fileField' => $this->file("fileField"),
'arrayField' => $this->input("arrayField"),
'arrayField.*.booleanField' => $this->boolean("arrayField.*.booleanField"),
'arrayField.*.fileField' => $this->file("arrayField.*.fileField"),
];
}
public function rules(): array
{
return [
"booleanField" => ["required", "boolean"], // <= works as expected
"fileField" => ["required", "file", "mimes:jpg,png,jpeg,docx,xlsx,zip", "max:5120"], // <= works as expected
"arrayField" => ["required", "array"],
"arrayField.*.booleanField" => ["required", "boolean"], // <= not working, always returning error "The arrayField.0.booleanField field must be true or false."
"arrayField.*.fileField" => ["required", "file", "mimes:jpg,png,jpeg,docx,xlsx,zip", "max:5120"], // <= not working, always returning error "The arrayField.0.fileField is required."
];
}
}
that’s what I found. I don’t know if any other rules also not working.
Laravel version 11.31.0.
Thank you.
duplicated question from #53489
2
Answers
The base problem is from client request to my API that using
Content-Type: multipart/form-data
headerAfter many hours workaround and based on explanation given by @IGP. This is the solution (probably).
reworked my FormRequest class:
Now all working as expected.
Maybe not the best for performance. You can always improve that.
Thank you... I hope this helps someone with similar case
The method
validationData
is supposed to be a way for you to access the data that is going to be validated. It’s not meant to override data.When a
FormRequest
is resolved, the application (or container if you prefer) calls itsvalidateResolved
method.If you want to modify the data that is about to get validated, the method you’re looking for is
prepareForValidation
. And to my knowledge you can’t use * like a wildcard there.