PHP code example of dive-be / laravel-dry-requests

1. Go to this page and download the library: Download dive-be/laravel-dry-requests 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/ */

    

dive-be / laravel-dry-requests example snippets


return [
    /*
    |--------------------------------------------------------------------------
    | Default Dry Validation Behavior
    |--------------------------------------------------------------------------
    |
    | All dry requests are validated against a subset of the defined rules.
    | In other words only present fields are checked during the request.
    | You may choose to halt validation as soon as a failure occurs,
    | or continue validating all fields and return all failures.
    |
    | Supported: "all", "first"
    |
    */

    'validation' => 'first',
];

class UserController
{
    public function store(StoreUserRequest $request): UserResource
    {
        $user = User::create($request->validated());

        return new UserResource($user);
    }
}

class StoreUserRequest extends FormRequest
{
    use DryRunnable;

    public function rules(): array
    {
        return [
            'email' => ['255'],
        ];
    }
}

class UserController
{
    public function store(Request $request): UserResource
    {
        $validated = $request->validate([
            'email' => ['],
        ]);

        $user = User::create($request->validated());

        return new UserResource($user);
    }
}

#[Dry(Validation::AllFailures)]
public function rules(): array
{
    return [...];
}

axios.post('/users', { email: '...', username: '...' }, { headers: { 'X-Dry-Run': 'all' } })
     .then(response => response.status); // 200 OK

class CreateUserRequest extends FormRequest
{
    protected function passedValidation()
    {
        $this->stopWhenDry(); // must be called first

        // your custom logic
    }

    protected function withValidator(Validator $instance)
    {
        $instance = /* your custom logic */;

        $this->withDryValidator($instance); // must be called last
    }
}
bash
php artisan vendor:publish --provider="Dive\DryRequests\ServiceProvider" --tag="config"