ygreis / laravel-validators

Custom validations for the laravel framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Validators

Custom validations for the laravel framework

📝 Summary

Requires:

1: First, you may use Composer to install Laravel Validators as a dependency into your Laravel project:

composer require ygreis/laravel-validators

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.


Validator file example (click here)

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

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());   
    }
}

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());
    }
}

You can use Laravel's Requests to validate the data and you can use Validators too.


Request file example (click here)

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];
    }

}


Usage Request in Controller

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:

Get request errors in the view
@if($errors->any())
    {!! implode('', $errors->all('<div>:message</div>')) !!}
@endif

We provide a package so you can test all the examples mentioned here, just access the link below :)

Click Here

About

Custom validations for the laravel framework

License:MIT License


Languages

Language:PHP 100.0%