Validating Form Arrays
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.
Clone repository to your drive and type in terminal there:
cp .env.example .env
and change the
php artisan key:generate
Create empty database file called
Run migrations and seed database with:
php artisan migrate --seed
From terminal type:
php artisan serve
You should get an address to open in your browser like http://localhost:8000.