This is a slightly modified version of KaneCohen's laravel-validation library that focuses on displaying individual error messages and utilizing Laravel's validation language file.
- Add the following to your
requirecomposer.jsonfile:
"cohensive/validation": "dev-master",
"sandyandi/validation": "1.0.*@dev"
- Run
composer updateand wait for it to download and install. - Open
app/config/app.php, comment out Laravel's default validation service provider:'Illuminate\Validation\ValidationServiceProvider',and add this library's service provider:'Sandyandi\Validation\ValidationServiceProvider',
The core usage documentation is found here: laravel-validation
The modification made to this library are the following:
- You can use indexes in your validation rules array, eg:
$rules = array(
'income_sources:0:employer_name' => 'required',
'income_sources:1:employer_name' => 'required'
);If the validation fails, you can know which employer_name failed the validation as it has an index and you can show its individual error message using something like:
$errors->has('income_sources:0:employer_name');...or
$errors->first('income_sources:0:employer_name');Tip: Use a loop to create those indexes.
- You can use the validation language file (
app/lang/en/validation.php) like the following:
'custom' => array(
'income_sources:employer_name' => 'Please let us know who your employer is.'
)...and
'attributes' => array(
'income_sources:employer_name' => 'employer name.'
)And those language keys will refer to validation rule keys like 'income_sources:0:employer_name', 'income_sources:1:employer_name' (whatever index you specify) or 'income_sources:*:employer_name'