Validating Form Arrays

Handling form array validation is no longer a pain in the ass, as it was with Laravel 4.



Introduction

It happened to me many times before. I would write a sick new React.js component or glued together a quick jQuery script to enable users to add multiple instances of the same type on one form and then the dilemma would occur: "Should I just add array in validation rules for that field or would I create a custom validation rule for that specific type of filed?".

The latest form that I am currently working on has the ability to add multiple device groups in which you can select the device type and enter the quantity for that device type. Meaning that I would have to have a exists validation rule for every device type field and an integer check for quantity with some other validation rules.

In this lesson I will skip the JS part of generating the form and will show you how the form looks like, how the HTML should be formatted, how to write validation rules for form array and some extra tips on how to avoid multiple exists calls to the database that could slow down your application.

Credits


To continue reading, choose a subscription plan:

Comments