Skip to content

Commit 9306ef3

Browse files
committed
Initial Commit
0 parents  commit 9306ef3

File tree

9 files changed

+253
-0
lines changed

9 files changed

+253
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.md diff=markdown
2+
*.php diff=php
3+
4+
/.editorconfig export-ignore
5+
/.gitignore export-ignore
6+
/.gitattributes export-ignore
7+
/phpunit.xml.dist export-ignore
8+
/tests export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.lock
3+
.phpunit.result.cache
4+
.DS_Store

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Johan Rosenson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<p align="center">
2+
<a href="https://packagist.org/packages/devlop/laravel-failed-validation-response"><img src="https://img.shields.io/packagist/v/devlop/laravel-failed-validation-response" alt="Latest Stable Version"></a>
3+
<a href="https://github.com/devlop/laravel-failed-validation-response/blob/master/LICENSE.md"><img src="https://img.shields.io/packagist/l/devlop/laravel-failed-validation-response" alt="License"></a>
4+
</p>
5+
6+
# Laravel Failed Validation Response
7+
8+
Trait for [Laravel FormRequests](https://laravel.com/docs/8.x/validation#form-request-validation)
9+
to enable the use of custom responses for failed validation (or authorization) attempts.
10+
11+
# Installation
12+
13+
```bash
14+
composer require devlop/laravel-failed-validation-response
15+
```
16+
17+
# Usage
18+
19+
```php
20+
use Devlop\Laravel\Validation\FailedValidationResponse;
21+
use Illuminate\Contracts\Validation\Validator;
22+
use Illuminate\Foundation\Http\FormRequest;
23+
use Symfony\Component\HttpFoundation\Response;
24+
25+
class DemoRequest extends FormRequest
26+
{
27+
use FailedValidationResponse;
28+
29+
// ... Your normal FormRequest logic.
30+
31+
/**
32+
* Get the response for a failed validation attempt.
33+
*
34+
* @param Validator $validator
35+
* @return Response|null
36+
*/
37+
public function failedValidationResponse(Validator $validator) : ?Response
38+
{
39+
// Implement this method to use a custom response on validation failure.
40+
// Do not implement this method to instead use the default behaviour.
41+
42+
// Example:
43+
return redirect()->to('/');
44+
}
45+
46+
/**
47+
* Get the response for a failed authorization attempt.
48+
*
49+
* @param Validator $validator
50+
* @return Response|null
51+
*/
52+
public function failedAuthorizationResponse(Validator $validator) : ?Response
53+
{
54+
// Implement this method to use a custom response on authorization failure.
55+
// Do not implement this method to instead use the default behaviour.
56+
57+
// Example:
58+
return response()->json([
59+
'reason' => 'you are not allowed to that!',
60+
], 401);
61+
}
62+
}
63+
```

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "devlop/laravel-failed-validation-response",
3+
"description": "Trait for Laravel FormRequest to enable the use of custom responses for failed validation attempts.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Johan Rosenson",
9+
"email": "johan@devlop.se"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.4|^8.0",
14+
"laravel/framework": "^8.0"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^9.5",
18+
"symfony/var-dumper": "^5.2"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Devlop\\Laravel\\Validation\\": "src/"
23+
}
24+
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"Devlop\\Laravel\\Validation\\Tests\\": "tests/"
28+
}
29+
},
30+
"minimum-stability": "dev",
31+
"prefer-stable": true,
32+
"extra": {
33+
"branch-alias": {
34+
"dev-master": "1.x-dev"
35+
}
36+
}
37+
}

phpunit.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
verbose="true">
7+
<testsuites>
8+
<testsuite name="devlop/laravel-failed-validation-response Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<coverage processUncoveredFiles="true">
13+
<include>
14+
<directory suffix=".php">./src</directory>
15+
</include>
16+
</coverage>
17+
</phpunit>

src/FailedValidationResponse.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devlop\Laravel\Validation;
6+
7+
use Illuminate\Contracts\Validation\Validator;
8+
use Illuminate\Http\Exceptions\HttpResponseException;
9+
use Illuminate\Validation\ValidationException;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
trait FailedValidationResponse
13+
{
14+
/**
15+
* Handle a failed validation attempt.
16+
*
17+
* @param Validator $validator
18+
* @return void
19+
*
20+
* @throws ValidationException
21+
*/
22+
protected function failedValidation(Validator $validator)
23+
{
24+
$response = $this->failedValidationResponse($validator);
25+
26+
if ($response) {
27+
throw new ValidationException($validator, $response);
28+
}
29+
30+
parent::failedValidation();
31+
}
32+
33+
/**
34+
* Get the response for a failed validation attempt.
35+
*
36+
* @param Validator $validator
37+
* @return Response|null
38+
*/
39+
protected function failedValidationResponse(Validator $validator) : ?Response
40+
{
41+
// Implement this method to use a custom response on validation failure.
42+
// Do not implement this method to instead use the default behaviour.
43+
44+
return null;
45+
}
46+
47+
/**
48+
* Handle a failed authorization attempt.
49+
*
50+
* @return void
51+
*
52+
* @throws HttpResponseException
53+
* @throws \Illuminate\Validation\UnauthorizedException
54+
*/
55+
protected function failedAuthorization()
56+
{
57+
$response = $this->failedAuthorizationResponse();
58+
59+
if ($response) {
60+
throw new HttpResponseException($response);
61+
}
62+
63+
// No custom response available, fall back to the default behaviour.
64+
parent::failedAuthorization();
65+
}
66+
67+
/**
68+
* Get the response for a failed authorization attempt.
69+
*
70+
* @return Response|null
71+
*/
72+
protected function failedAuthorizationResponse() : ?Response
73+
{
74+
// Implement this method to use a custom response on authorization failure.
75+
// Do not implement this method to instead use the default behaviour.
76+
77+
return null;
78+
}
79+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devlop\Laravel\Validation\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class FailedValidationResponseTest extends TestCase
10+
{
11+
public function test_placeholder() : void
12+
{
13+
$this->markTestIncomplete('This test has not been implemented yet.');
14+
}
15+
}

0 commit comments

Comments
 (0)