Reputation: 63
This is most likely a duplicate question but I have been looking for solutions and can't seem to find one that fixes the issue I have. I have created a function called validateRequest which validates all the fields. This function is then called in store function $post = Post::create($this->validateRequest());
I have made sure in the HTML form enctype="multipart/form-data"
has been included, but every time a submit a new entry that errors appear. Not sure if I am using the return tap method correctly or something else, really appreciate some help thanks.
public function validateRequest()
{
return tap(
$validated = request()->validate([
'title' => 'required|string',
'h1' => 'required|string',
'page_title' => 'required|string',
'meta_description' => 'required|string',
'image' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
'content' => 'required|string',
'active' => 'integer'
]), function () {
if (request()->hasFile('image')){
request()->validate([
'image' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
]);
}
// Check if active is ticked
$validated['active'] = isset(request()->active[0]) ? 1 : 0;
// Create slug from title
$validated['slug'] = Str::slug(request()['title'], '-');
});
}
Upvotes: 1
Views: 2124
Reputation: 2951
"files"
is not a valid validator, use file
without "s"
'image' => 'sometimes|files|image|mimes:jpeg,png,jpg,gif,svg|max:5000',
Hope this helps
Upvotes: 1