Handling form array validation is no longer a pain in the ass as it was with Laravel 4.
PHP HTML JavaScript Other
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
app
bootstrap
config
database
public
resources
routes
storage
tests
.env.example
.gitattributes
.gitignore
artisan
composer.json
composer.lock
gulpfile.js
package.json
phpunit.xml
readme.md
server.php

readme.md

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.

Installation

Clone repository to your drive and type in terminal there:

composer install

Configuration

Copy file .env.example to .env file:

cp .env.example .env

and change the APP_KEY in .env using:

php artisan key:generate

Create empty database file called database.sqlite in database directory.

Run migrations and seed database with:

php artisan migrate --seed

Running

From terminal type:

php artisan serve

You should get an address to open in your browser like http://localhost:8000.

Have fun!