laravel-validators maintained by ygreis
Laravel Validators
Custom validations for the laravel framework
📝 Summary
✨ Getting Started
Installation
Requires:
1: First, you may use Composer to install Laravel Validators as a dependency into your Laravel project:
composer require ygreis/laravel-validators
How to use?
Basic usage
The package provides two simple functions that can be used, one where errors are returned makeValidator and the other, if an error occurs, a custom exception is thrown that also returns errors makeValidatorThrow.
Create a file in a folder, example:
\App\Validators\TestValidator
<?php
namespace App\Validators;
use Ygreis\LaravelValidators\AbstractValidator;
class TestValidator extends AbstractValidator
{
public function messages(): array
{
return [];
}
public function rules(): array
{
return [
'name' => 'required|string',
'age' => 'nullable|integer|min:18|max:120',
];
}
public function attributes(): array
{
return [
'name' => 'Name',
'age' => 'Age',
];
}
public function validators(): array
{
// You can import other validators Here
return [
// TestTwoValidator::class
];
}
}
After creating a validation file, see how to use it in the examples below
Function makeValidator
Example of use:
public function TestValidatorException(Request $request)
{
// Imagine that your Validator is in the path \App\Validators\TestValidator
$validate = makeValidator(\App\Validators\TestValidator::class, $request->all());
// Check has errors
if ($validate->errors()->count()) {
dd('Get all errors', $validate->errors()->all());
}
}
Function makeValidatorThrow
Example of use:
public function TestValidatorException(Request $request)
{
try {
makeValidatorThrow(\App\Validators\TestValidator::class, $request->all());
dd('All right!');
// Exception from the library where it will be triggered when the Validator fails
} catch (ValidatorException $exception) {
// $exception->getMessage() Returns the first validation message that did not pass.
// $exception->getErrors()->messages()->all() Returns all validation messages that did not pass.
dd($exception->getMessage(), $exception->getErrors()->messages()->all());
}
}
With Requests
You can use Laravel's Requests to validate the data and you can use Validators too.
Create a file in a folder, example:
\App\Http\Requests\TestRequest
<?php
namespace App\Http\Requests;
use App\Validators\TestValidator;
use Ygreis\LaravelValidators\AbstractRequest;
class TestRequest extends AbstractRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'user.age' => 'nullable|integer|min:18|max:120',
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'user.age' => 'User Age',
];
}
public function validators()
{
return [TestValidator::class];
}
}
Create a controller in a folder, example:
\App\Http\Controllers
<?php
namespace App\Http\Controllers;
use App\Http\Requests\TestRequest;
class TestController extends Controller {
public function TestValidatorException(TestRequest $request)
{
dd('All Right');
}
}
When the data is invalid, by default it will redirect to the previous page showing the errors that occurred, to catch the errors in the view you can do something like this:
@if($errors->any())
{!! implode('', $errors->all('<div>:message</div>')) !!}
@endif
Example project
We provide a package so you can test all the examples mentioned here, just access the link below :)