Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit bd59c3f

Browse files
pascalbaljetarnonm
andauthored
Spoof method boolean + test (#7)
Co-authored-by: arnonm <[email protected]> Co-authored-by: Pascal Baljet <[email protected]>
1 parent b44bce8 commit bd59c3f

File tree

6 files changed

+79
-11
lines changed

6 files changed

+79
-11
lines changed
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<form method="{{ $method }}" {!! $attributes->merge([
1+
<form method="{{ $spoofMethod ? 'POST' : $method }}" {!! $attributes->merge([
22
'class' => $hasError() ? 'needs-validation' : ''
33
]) !!}>
44
<style>
@@ -7,9 +7,13 @@
77
}
88
</style>
99

10-
@unless(in_array($method, ['HEAD', 'GET', 'OPTIONS']))
11-
@csrf
12-
@endunless
10+
@unless(in_array($method, ['HEAD', 'GET', 'OPTIONS']))
11+
@csrf
12+
@endunless
13+
14+
@if($spoofMethod)
15+
@method($method)
16+
@endif
1317

1418
{!! $slot !!}
15-
</form>
19+
</form>
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
<form method="{{ $method }}" {!! $attributes !!}>
2-
@unless(in_array($method, ['HEAD', 'GET', 'OPTIONS']))
3-
@csrf
4-
@endunless
1+
<form method="{{ $spoofMethod ? 'POST' : $method }}" {!! $attributes !!}>
2+
@unless(in_array($method, ['HEAD', 'GET', 'OPTIONS']))
3+
@csrf
4+
@endunless
5+
6+
@if($spoofMethod)
7+
@method($method)
8+
@endif
59

610
{!! $slot !!}
7-
</form>
11+
</form>

src/Components/Form.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ class Form extends Component
1212
*/
1313
public string $method;
1414

15+
/**
16+
* Form method spoofing to support PUT, PATCH and DELETE actions.
17+
* https://laravel.com/docs/master/routing#form-method-spoofing
18+
*/
19+
public bool $spoofMethod = false;
20+
1521
/**
1622
* Create a new component instance.
1723
*
@@ -20,6 +26,8 @@ class Form extends Component
2026
public function __construct(string $method = 'POST')
2127
{
2228
$this->method = strtoupper($method);
29+
30+
$this->spoofMethod = in_array($this->method, ['PUT', 'PATCH', 'DELETE']);
2331
}
2432

2533
/**

tests/Feature/SpoofMethodTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelFormComponents\Tests\Feature;
4+
5+
use ProtoneMedia\LaravelFormComponents\Tests\TestCase;
6+
7+
class SpoofMethodTest extends TestCase
8+
{
9+
/** @test */
10+
public function it_spoofs_the_methods_for_put_patch_and_delete_forms()
11+
{
12+
$this->registerTestRoute('spoof-method')
13+
->visit('/spoof-method')
14+
15+
// methods
16+
->seeElement('form[id="form_get"][method="GET"]')
17+
->seeElement('form[id="form_post"][method="POST"]')
18+
->seeElement('form[id="form_put"][method="POST"]')
19+
->seeElement('form[id="form_patch"][method="POST"]')
20+
->seeElement('form[id="form_delete"][method="POST"]')
21+
22+
// spoofing
23+
->dontSeeElement('form[id="form_get"] input[name="_method"]')
24+
->dontSeeElement('form[id="form_post"] input[name="_method"]')
25+
->seeElement('form[id="form_put"] input[name="_method"][value="PUT"]')
26+
->seeElement('form[id="form_patch"] input[name="_method"][value="PATCH"]')
27+
->seeElement('form[id="form_delete"] input[name="_method"][value="DELETE"]')
28+
29+
// csrf
30+
->dontSeeElement('form[id="form_get"] input[name="_token"]')
31+
->seeElement('form[id="form_post"] input[name="_token"]')
32+
->seeElement('form[id="form_put"] input[name="_token"]')
33+
->seeElement('form[id="form_patch"] input[name="_token"]')
34+
->seeElement('form[id="form_delete"] input[name="_token"]');
35+
}
36+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<x-form id="form_get" method="GET">
2+
</x-form>
3+
4+
<x-form id="form_post" method="POST">
5+
</x-form>
6+
7+
<x-form id="form_put" method="PUT">
8+
</x-form>
9+
10+
<x-form id="form_patch" method="PATCH">
11+
</x-form>
12+
13+
<x-form id="form_delete" method="DELETE">
14+
</x-form>

tests/TestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function getPackageProviders($app)
2828
return [ServiceProvider::class, LivewireServiceProvider::class];
2929
}
3030

31-
protected function registerTestRoute($uri, callable $post = null)
31+
protected function registerTestRoute($uri, callable $post = null): self
3232
{
3333
Route::middleware('web')->group(function () use ($uri, $post) {
3434
Route::view($uri, $uri);
@@ -37,5 +37,7 @@ protected function registerTestRoute($uri, callable $post = null)
3737
Route::post($uri, $post);
3838
}
3939
});
40+
41+
return $this;
4042
}
4143
}

0 commit comments

Comments
 (0)