Skip to content

Commit f6d1e0b

Browse files
authored
Merge pull request #116 from bolt/feature/redirect-self
New Feature: Redirect to 'self'
2 parents 485b88d + 3367046 commit f6d1e0b

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

docs/handlers/redirect.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Redirect after a succesful submit
22
=================================
33

4-
After the form has been successfully submitted, the visitor will normally get
5-
redirected to the originating page. You can use the Redirect handler to send
6-
them elsewhere, instead.
4+
After the form has been successfully submitted, the visitor will normally get
5+
redirected to the originating page. You can use the Redirect handler to send
6+
them elsewhere, instead.
77

8-
You can do this, by setting `redirect:` in the `feedback:` section of the
8+
You can do this, by setting `redirect:` in the `feedback:` section of the
99
form's configuration.
1010

1111

@@ -18,19 +18,24 @@ feedback:
1818
query: [name, email]
1919
```
2020
21-
The `target:` specifies where the visitor will be sent. Note that you can add
22-
optional get parameters in this URI, that will get sent as-is. For example:
21+
The `target:` specifies where the visitor will be sent. Note that you can add
22+
optional get parameters in this URI, that will get sent as-is. For example:
2323

2424
```yaml
2525
target: page/another-page?foo=bar&qux=boo
2626
```
2727

28-
The optional `query:` lets you set additional parameters that will contain the
29-
corresponding values from the form. For example, the configuration above will redirect the visitor to a URL like this, after a correct form has been posted:
28+
You can use 'self' as a special case: This will redirect the browser (using a
29+
GET request) to the originating page. This is useful for when you want the user
30+
to stay on the same page, without them being able to accidentally re-submit the
31+
form by refreshing the page.
32+
33+
The optional `query:` lets you set additional parameters that will contain the
34+
corresponding values from the form. For example, the configuration above will redirect the visitor to a URL like this, after a correct form has been posted:
3035

3136
```
3237
/page/another-page?foo=bar&qux=boo&name=Bob&email=bob%40twokings.nl
3338
```
3439

35-
Please note that these will be sent as `GET` parameters, so do not use these to
40+
Please note that these will be sent as `GET` parameters, so do not use these to
3641
pass around sensitive information.

src/Event/PostSubmitEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getFormConfig(): Collection
6767
return new Collection($this->getConfig()->get($this->formName));
6868
}
6969

70-
public function getMeta()
70+
public function getMeta(): array
7171
{
7272
return [
7373
'ip' => $this->request->getClientIp(),

src/EventSubscriber/Redirect.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ private function makeUrl($redirect): string
6363
{
6464
$parsedUrl = parse_url($redirect['target']);
6565

66+
// Special case, if redirecting to 'self', get the current URL and return it
67+
if ($redirect['target'] == 'self') {
68+
return $this->event->getMeta()['path'];
69+
}
70+
6671
// parse_str returns result in `$query` ¯\_(ツ)_/¯
6772
parse_str($parsedUrl['query'] ?? '', $query);
6873

0 commit comments

Comments
 (0)