DEV Community

loading...
Cover image for Validating Multiple Forms on a Page in Laravel 8 - Announcement

Validating Multiple Forms on a Page in Laravel 8 - Announcement

zaxwebs profile image Zack Webster Updated on ・1 min read

Laravel comes with quite an effective out-of-the-box solution for validation. Most of the documentation covers handling validation for a single form on a page. But what if you have many forms? Chances are you might have some fields with the same name attribute.

Taking a look at Google, the most common solution I came across is to prefix the field names like login_username i.e in form_name_field_name format. Which works, however, doing so means now you have to tell Laravel to do something like:

 $this->validate($request,
            [
                'post_body' => 'required_without:post_image|max:1000',
                'post_image' => 'required_without:post_body|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
            ],
            [],
            [
                'post_body' => 'post',
                'post_image' => 'attached file',
            ]);
Enter fullscreen mode Exit fullscreen mode

So, I set out to experiment a bit and come up with other solutions. One of my first attempts at this is up on my GitHub.
Check out the zaxwebs/ex-l8-multi-forms repo.

Discussion (0)

pic
Editor guide