PHP code example of sands / laravel-controller-validation

1. Go to this page and download the library: Download sands/laravel-controller-validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sands / laravel-controller-validation example snippets


'providers' => [
     ...
     Sands\Validation\ValidationServiceProvider::class,
     ...
]

protected $routeMiddleware = [
    ...
    'validation' => Sands\Validation\ValidationMiddleware::class,
    ...
]

Route::resource('users', 'UsersController');

// in app/Http/Controllers/UsersController.php

public function __construct()
{
    $this->middleware('validation');
}

Route::group(['middleware' => ['validation']], function() {
    Route::resource('users', 'UsersController');
});

// in app/Http/Controllers/UsersController.php

use App\Validations\UsersControllerRules;

...

public function __construct()
{
    $this->middleware('validation:' . UsersControllerRules::class);
}

@if (count($errors) > 0)
<div class="alert alert-danger">
    <ul>
    @foreach ($errors->all() as $error)
        <li>{{$error}}</li>
    @endforeach
    </ul>
</div>
@endif

// app/Validations/UsersControllerRules.php
...
protected function storeData($request, $params)
{
    return $request->only('username');
}

protected function store($request, $params)
{
    return [
        'username' => '
json
{
    "errors": [validation errors]
}