Skip to content

Commit

Permalink
Preserve old input on error
Browse files Browse the repository at this point in the history
  • Loading branch information
BytewaveMLP committed Jul 8, 2017
1 parent 641bfce commit b598c39
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bootstrap/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

$app->add(new SubCompare\Middleware\ActiveRouteMiddleware($container));
$app->add(new \SubCompare\Middleware\ActiveRouteMiddleware($container));
$app->add(new \SubCompare\Middleware\OldInputMiddleware($container));
4 changes: 2 additions & 2 deletions resources/views/pages/home.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
<form action="{{ path_for('compare') }}" method="get">
<div class="form-group">
<label for="user1">User 1</label>
<input type="text" class="form-control" id="user1" name="user1" placeholder="UCXXXXXXXXXXXXXXXXXXXXXX">
<input type="text" class="form-control" id="user1" name="user1" placeholder="UCXXXXXXXXXXXXXXXXXXXXXX" value="{{ old.user1 }}">
<p class="help-block">
This is the YouTube user's <b>channel ID</b>, which should start with <code>UC</code>. If you aren't sure how to get that, please <a href="https://imgur.com/a/P6Dcm" target="_blank">click here</a>.
</p>
</div>

<div class="form-group">
<label for="user2">User 2</label>
<input type="text" class="form-control" id="user2" name="user2" placeholder="UCXXXXXXXXXXXXXXXXXXXXXX">
<input type="text" class="form-control" id="user2" name="user2" placeholder="UCXXXXXXXXXXXXXXXXXXXXXX" value="{{ old.user2 }}">
</div>

<button type="submit" class="btn btn-default">Submit</button>
Expand Down
23 changes: 23 additions & 0 deletions src/Middleware/OldInputMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

namespace SubCompare\Middleware;

/**
* Persists old input for forms that request it
*/
class OldInputMiddleware extends Middleware
{
public function __invoke($request, $response, $next) {
$this->container->view->getEnvironment()->addGlobal('old', $_SESSION['old'] ?? null);
$_SESSION['old'] = $request->getParams();

$response = $next($request, $response);
return $response;
}
}

0 comments on commit b598c39

Please sign in to comment.