Skip to content

Commit

Permalink
Add ability to redirect after form submission (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored Jun 17, 2024
1 parent cfac89e commit 246a8f5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import Clipboard from '@ryangjchandler/alpine-clipboard'
+ import "../../vendor/aerni/livewire-forms/resources/dist/js/livewire-forms";

Alpine.plugin(Clipboard)

Livewire.start()
```

Expand All @@ -47,14 +47,14 @@ Then, add the `{{ livewire:styles }}` and `{{ livewire:scriptConfig }}` tags to
<head>
<!-- Antlers -->
{{ livewire:styles }}

<!-- Blade -->
@livewireStyles
</head>
<body>
<!-- Antlers -->
{{ livewire:scriptConfig }}

<!-- Blade -->
@livewireScriptConfig
</body>
Expand Down Expand Up @@ -128,11 +128,12 @@ Use the `view` and `theme` parameters if you want to use a view or theme that is

#### Available Properties

| Property | Description |
| -------- | ------------------------------------------------- |
| `handle` | The handle of the form you want to use (required) |
| `view` | The component view you want to use (optional) |
| `theme` | The theme you want to use (optional) |
| Property | Description |
| ---------- | --------------------------------------------------------------------- |
| `handle` | The handle of the form you want to use (required) |
| `view` | The component view you want to use (optional) |
| `theme` | The theme you want to use (optional) |
| `redirect` | Redirect the user to this URL after successfull submission (optional) |

## Views

Expand Down Expand Up @@ -419,7 +420,7 @@ Lastly, we need to render our new `ContactForm` component in the template.

### Validation Rules

You can use any validation rule you want. Simply add it to the field in the form blueprint or use the blueprint builder in the CP.
You can use any validation rule you want. Simply add it to the field in the form blueprint or use the blueprint builder in the CP.

To validate against the value of another field, you need to get its value like in the following example:

Expand Down
2 changes: 1 addition & 1 deletion resources/views/dynamic-form.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div>
<livewire:dynamic-component :is="$this->component" :$handle :$theme :$type :$view />
<livewire:dynamic-component :is="$this->component" :$handle :$theme :$type :$view :$redirect />
</div>
1 change: 1 addition & 0 deletions src/Commands/form.stub
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class [class] extends BaseForm
// $this->theme = 'secondary';
// $this->type = 'wizard';
// $this->view = 'default';
// $this->redirect = 'thank-you';
// }

/*
Expand Down
2 changes: 2 additions & 0 deletions src/Livewire/BaseForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Aerni\LivewireForms\Livewire\Concerns\WithForm;
use Aerni\LivewireForms\Livewire\Concerns\WithHandle;
use Aerni\LivewireForms\Livewire\Concerns\WithMessages;
use Aerni\LivewireForms\Livewire\Concerns\WithRedirect;
use Aerni\LivewireForms\Livewire\Concerns\WithSections;
use Aerni\LivewireForms\Livewire\Concerns\WithSteps;
use Aerni\LivewireForms\Livewire\Concerns\WithTheme;
Expand All @@ -27,6 +28,7 @@ class BaseForm extends Component
use WithSections;
use WithSteps;
use WithMessages;
use WithRedirect;
use SubmitsForm;

public function render(): View
Expand Down
7 changes: 6 additions & 1 deletion src/Livewire/Concerns/HandlesSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

trait HandlesSuccess
{
protected function handleSuccess(): self
protected function handleSuccess(): ?self
{
session()->flash('success', $this->successMessage());
session()->flash('submission', $this->submission);

if (! empty($this->redirect)) {
return $this->redirect($this->redirect);
}

if (! $this->isWizardForm()) {
$this->resetForm();
Expand Down
11 changes: 11 additions & 0 deletions src/Livewire/Concerns/WithRedirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Aerni\LivewireForms\Livewire\Concerns;

use Livewire\Attributes\Locked;

trait WithRedirect
{
#[Locked]
public string $redirect = '';
}
2 changes: 2 additions & 0 deletions src/Livewire/DynamicForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Aerni\LivewireForms\Livewire\Concerns\WithComponent;
use Aerni\LivewireForms\Livewire\Concerns\WithHandle;
use Aerni\LivewireForms\Livewire\Concerns\WithRedirect;
use Aerni\LivewireForms\Livewire\Concerns\WithTheme;
use Aerni\LivewireForms\Livewire\Concerns\WithType;
use Aerni\LivewireForms\Livewire\Concerns\WithView;
Expand All @@ -17,6 +18,7 @@ class DynamicForm extends Component
use WithTheme;
use WithType;
use WithView;
use WithRedirect;

public function render(): View
{
Expand Down

0 comments on commit 246a8f5

Please sign in to comment.