Skip to content

This package will allow you to remove all manual validations from your controllers.

Notifications You must be signed in to change notification settings

hamdallah90/laravel-auto-validation

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Laravel Auto-validation

This package will allow you to remove all manual validations from your controllers.

Installation

Simply go to your project directory where the composer.json file is located and type in your terminal:

composer require bekusc/laravel-auto-validation

Add the service provider to your providers array in config/app.php:

'providers' => [
    // ...
    Bekusc\Validation\AutoValidationProvider::class,
],

Add the trait to your base controller App\Http\Controllers\Controller:

use Bekusc\Validation\Traits\AutoValidation;

class Controller extends BaseController
{
    use AutoValidation;
}

Publish config:

php artisan vendor:publish --provider="Bekusc\Validation\AutoValidationProvider"

Update config file with your validation rules in:

config/validation.php

Example

config/validation.php file:

use Illuminate\Validation\Rule;

$rules = [
    'UserController' => [
        'register' => [
            'name'     => 'required|max:255',
            'email'    => ['required', 'email', 'max:255', Rule::unique('users')->where('status', 1)],
            'password' => 'required|min:6|confirmed',
            'gender'   => 'required|in:male,female',
            'birthday' => 'required|date_format:Y-n-j',
        ],
        'update' => function ($request) {
            return [
                'name'     => 'required|max:255',
                'email'    => 'required|email|max:255|unique:users,email,'.$request->user()->id,
                'gender'   => 'required|in:male,female',
                'birthday' => 'required|date_format:Y-n-j',
            ];
        },
    ],
];

return ['rules' => $rules];

UserController.php file:

class UserController extends Controller
{
    public function register(Request $request)
    {
        // The incoming request is valid...
        User::create($request->all());
    }

    public function update(Request $request)
    {
        // The incoming request is valid...
        $request->user()->update($request->all());
    }
}

Note

If validation fails, a redirect response will be generated to send the user back to their previous location. The errors will also be flashed to the session so they are available for display. If the request was an AJAX request, a HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors.

About

This package will allow you to remove all manual validations from your controllers.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%