summaryrefslogtreecommitdiff
path: root/src/Middleware
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2019-07-09 21:43:18 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2019-07-09 21:43:18 +0200
commit7414f9b23dbcc66e5f0efda3d0cbfd79372ec780 (patch)
tree891a11b71d8b25922fc4b343dbc77a01a0646ba1 /src/Middleware
parent508695efb253d7bc0caea1fa017ed5608d774596 (diff)
Implemented Validation for controllers
Diffstat (limited to 'src/Middleware')
-rw-r--r--src/Middleware/ErrorHandler.php30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/Middleware/ErrorHandler.php b/src/Middleware/ErrorHandler.php
index 29b1fac1..c89edb1a 100644
--- a/src/Middleware/ErrorHandler.php
+++ b/src/Middleware/ErrorHandler.php
@@ -3,6 +3,8 @@
namespace Engelsystem\Middleware;
use Engelsystem\Http\Exceptions\HttpException;
+use Engelsystem\Http\Exceptions\ValidationException;
+use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@@ -43,6 +45,21 @@ class ErrorHandler implements MiddlewareInterface
$response = $handler->handle($request);
} catch (HttpException $e) {
$response = $this->createResponse($e->getMessage(), $e->getStatusCode(), $e->getHeaders());
+ } catch (ValidationException $e) {
+ $response = $this->createResponse('', 302, ['Location' => $this->getPreviousUrl($request)]);
+
+ if ($request instanceof Request) {
+ $session = $request->getSession();
+ $session->set(
+ 'errors',
+ array_merge_recursive(
+ $session->get('errors', []),
+ ['validation' => $e->getValidator()->getErrors()]
+ )
+ );
+
+ $session->set('form-data', $request->request->all());
+ }
}
$statusCode = $response->getStatusCode();
@@ -106,4 +123,17 @@ class ErrorHandler implements MiddlewareInterface
{
return response($content, $status, $headers);
}
+
+ /**
+ * @param ServerRequestInterface $request
+ * @return string
+ */
+ protected function getPreviousUrl(ServerRequestInterface $request)
+ {
+ if ($header = $request->getHeader('referer')) {
+ return array_pop($header);
+ }
+
+ return '/';
+ }
}