Answer the question
In order to leave comments, you need to log in
How to properly validate on a multiupload image?
$data = Request::only('images');
$rules = [
'images' => 'required|mimes:png,jpg,jpeg,gif,svg'
];
// dd($data['images']);
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
echo 'false';
} else {
echo 'true';
}
die();
array:2 [▼
0 => UploadedFile {#29 ▼
-test: false
-originalName: "owner.jpg"
-mimeType: "image/jpeg"
-size: 6818
-error: 0
}
1 => UploadedFile {#30 ▼
-test: false
-originalName: "port.png"
-mimeType: "image/png"
-size: 3710
-error: 0
}
]
// они есть , значит форма в html настроенна true
Answer the question
In order to leave comments, you need to log in
$rules = [
// тут правила для всех полей кроме картинок
];
$validator = Validator::make($request->all(), $rules);
$validator->each('images', ['required', 'mimes:png,jpg,jpeg,gif,svg']);
if ($validator->fails()) {
// ...
}
namespace AppHttpRequests;
use AppHttpRequestsRequest;
class ImagesRequest extends Request {
public function authorize()
{
return true; // если гости тоже могут загружать картинки то false
}
public function rules()
{
return [
// тут правила для всех полей кроме картинок
];
foreach($this->request->file('images') as $key => $val)
{
$rules['image.'.$key] = 'required|mimes:png,jpg,jpeg,gif,svg';
}
return $rules;
}
}
public function store(App\Http\Requests\ImagesRequest $request)
{
// ...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question