Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.

Commit 073df6f

Browse files
committed
Submit input + added array test
1 parent e47c3c4 commit 073df6f

File tree

9 files changed

+73
-11
lines changed

9 files changed

+73
-11
lines changed

app/app/Http/Controllers/FormComponentsController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public function arrays()
4040
return view('form.components.arrays');
4141
}
4242

43+
public function submitValue()
44+
{
45+
return view('form.components.submitValue');
46+
}
47+
48+
public function submitValueSubmit(Request $request)
49+
{
50+
$data = $request->validate([
51+
'approve' => ['required', 'in:0,1'],
52+
]);
53+
54+
return redirect()->route('form.components.submitValue', ['approved' => $data['approve']]);
55+
}
56+
4357
public function defaults()
4458
{
4559
Select::defaultChoices();

app/resources/views/form/components/simple.blade.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ class="space-y-4"
1212
>
1313
<x-splade-input name="name" label="Name" />
1414

15-
16-
17-
<x-splade-input name="disabled" label="disabled" v-bind:disabled="!form.terms">
18-
@slot('append')
19-
<a href class="text-indigo-600 font-medium">GO!</a>
20-
@endslot
21-
</x-splade-input>
22-
2315
<x-splade-input name="password" label="Password" type="password" />
2416
<x-splade-input name="secret" type="hidden" />
2517

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@extends('layout')
2+
3+
@section('content')
4+
5+
FormComponents
6+
7+
<div class="max-w-sm mx-auto px-4">
8+
<x-splade-form class="space-y-4">
9+
<x-splade-submit name="approve" value="0" label="No" />
10+
<x-splade-submit name="approve" value="1" label="Yes" />
11+
</x-splade-form>
12+
</div>
13+
14+
@endsection

app/routes/web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
Route::get('form/components/unguarded', [FormComponentsController::class, 'unguarded'])->name('form.components.unguarded');
8888
Route::get('form/components/defaultUnguarded', [FormComponentsController::class, 'defaultUnguarded'])->name('form.components.defaultUnguarded');
8989
Route::post('form/components', [FormComponentsController::class, 'submit'])->name('form.components.submit');
90+
Route::get('form/components/submitValue/{approved?}', [FormComponentsController::class, 'submitValue'])->name('form.components.submitValue');
91+
Route::post('form/components/submitValue/{approved?}', [FormComponentsController::class, 'submitValueSubmit'])->name('form.components.submitValueSubmit');
9092

9193
Route::get('form/relations/belongsToMany', [FormRelationsController::class, 'belongsToMany'])->name('form.relations.belongsToMany');
9294
Route::post('form/relations/belongsToMany', [FormRelationsController::class, 'storeBelongsToMany'])->name('form.relations.storeBelongsToMany');

app/tests/Browser/Form/AttributesTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,22 @@ public function it_fills_undefined_attributes_from_default_php_data()
8080
->assertSeeIn('#all', '{ "name": "Test", "email": "", "description": {}, "agree": false, "options": [], "theme": "" }');
8181
});
8282
}
83+
84+
/** @test */
85+
public function it_handles_nested_numeric_keys()
86+
{
87+
$this->browse(function (Browser $browser) {
88+
$browser->visit('form/components/arrays')
89+
->waitForText('FormComponents')
90+
->assertInputValue('nested.key', 'value')
91+
->assertInputValue('nested[another]', 'key')
92+
->assertInputValue('list[1]', 'one')
93+
->assertInputValue('list[2]', 'two')
94+
->assertInputValue('list[3]', 'three')
95+
->assertInputValue('another.1', 'one')
96+
->assertInputValue('another.2', 'two')
97+
->assertInputValue('another.3', 'three')
98+
->assertSeeIn('#all', '{ "options": [], "nested": { "key": "value", "another": "key" }, "list": { "1": "one", "2": "two", "3": "three" }, "another": { "1": "one", "2": "two", "3": "three" } }');
99+
});
100+
}
83101
}

app/tests/Browser/Form/SimpleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,18 @@ public function it_submits_the_filled_elements()
5050
->assertRouteIs('navigation.one');
5151
});
5252
}
53+
54+
/** @test */
55+
public function it_can_submit_a_value_in_the_submit_button()
56+
{
57+
$this->browse(function (Browser $browser) {
58+
$browser->visit('form/components/submitValue')
59+
->waitForText('FormComponents')
60+
->press('No')
61+
->waitForRoute('form.components.submitValue', ['approved' => 0])
62+
->waitForText('FormComponents')
63+
->press('Yes')
64+
->waitForRoute('form.components.submitValue', ['approved' => 1]);
65+
});
66+
}
5367
}

lib/Components/Form.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ export default {
164164
return set(this.values, key, value);
165165
},
166166
167-
submit() {
167+
submit($event) {
168+
const submitter = $event.submitter;
169+
170+
if(submitter.name) {
171+
this.$put(submitter.name, submitter.value);
172+
}
173+
168174
if (!this.confirm) {
169175
return this.request();
170176
}

resources/views/form/submit.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'rounded-md shadow-sm bg-indigo-500 hover:bg-indigo-700 text-white font-bold py-2 px-4 focus:outline-none focus:shadow-outline'
33
)->merge([
44
'type' => $type
5-
]) }}
5+
])->when($name, fn($attr) => $attr->merge(['name' => $name, 'value' => $value])) }}
66
>
77
@if(trim($slot))
88
{{ $slot }}

src/Components/Form/Submit.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class Submit extends Component
1414
public function __construct(
1515
public string $label = 'Submit',
1616
public string $type = 'submit',
17-
public bool $spinner = true
17+
public bool $spinner = true,
18+
public string $name = '',
19+
public $value = null,
1820
) {
1921
}
2022

0 commit comments

Comments
 (0)