I am creating a recipe posting application with Laravel6.
I have implemented a mechanism to dynamically add an image submission form in JavaScript.
I set the validation in FormRequest, but the mandatory input validation for dynamically added form fields does not work.
blade
<input type="file" name="upload_image[cooking_image][]" class="howto-image" style="display:none" accept="image/*">
FormRequest
public function rules(Request $request)
{
return [
'upload_image.cooking_image.*' => 'required|image|mimes:jpeg,png,jpg',
];
}
But the following works.
FormRequest
public function rules(Request $request)
{
return [
'upload_image.cooking_image.0' => 'required|image|mimes:jpeg,png,jpg',
'upload_image.cooking_image.1' => 'required|image|mimes:jpeg,png,jpg',
'upload_image.cooking_image.2' => 'required|image|mimes:jpeg,png,jpg',
'upload_image.cooking_image.3' => 'required|image|mimes:jpeg,png,jpg',
'upload_image.cooking_image.4' => 'required|image|mimes:jpeg,png,jpg',
];
}
I want to make mandatory input validation work for all dynamically added form input fields.
I think it sometimes not really possible to make it easily dynamic. Personally, in those case, I update my rules array on the fly.
I think this should help you : How to use required_if on file array fields with an array for first argument?
Good luck!