diff --git a/app/app/Http/Controllers/FormComponentsController.php b/app/app/Http/Controllers/FormComponentsController.php index 07dd5b08..f1720eeb 100644 --- a/app/app/Http/Controllers/FormComponentsController.php +++ b/app/app/Http/Controllers/FormComponentsController.php @@ -31,6 +31,11 @@ public function vmodel() return view('form.components.vmodel', ['countries' => $this->countries()]); } + public function checkbox() + { + return view('form.components.checkbox'); + } + public function checkboxes() { return view('form.components.checkboxes', [ diff --git a/app/resources/views/form/components/checkbox.blade.php b/app/resources/views/form/components/checkbox.blade.php new file mode 100644 index 00000000..c673573e --- /dev/null +++ b/app/resources/views/form/components/checkbox.blade.php @@ -0,0 +1,35 @@ +@extends('layout') + +@section('content') + +FormCheckbox + +
+ + + + + + + + + + + +
@{{ form.$all }}
+
+
+ +@endsection \ No newline at end of file diff --git a/app/routes/web.php b/app/routes/web.php index b62dd5c4..4407e196 100644 --- a/app/routes/web.php +++ b/app/routes/web.php @@ -104,6 +104,7 @@ Route::get('form/components/simple', [FormComponentsController::class, 'simple'])->name('form.components.simple'); Route::get('form/components/vmodel', [FormComponentsController::class, 'vmodel'])->name('form.components.vmodel'); + Route::get('form/components/checkbox', [FormComponentsController::class, 'checkbox'])->name('form.components.checkbox'); Route::get('form/components/checkboxes', [FormComponentsController::class, 'checkboxes'])->name('form.components.checkboxes'); Route::post('form/components/checkboxes', [FormComponentsController::class, 'submitCheckboxes'])->name('form.components.submitCheckboxes'); Route::get('form/components/radios', [FormComponentsController::class, 'radios'])->name('form.components.radios'); diff --git a/app/tests/Browser/Form/ComponentsTest.php b/app/tests/Browser/Form/ComponentsTest.php index dde1b13e..c3615388 100644 --- a/app/tests/Browser/Form/ComponentsTest.php +++ b/app/tests/Browser/Form/ComponentsTest.php @@ -37,4 +37,47 @@ public function it_can_render_multiple_radios_with_an_options_array() ->assertRouteIs('navigation.one'); }); } + + /** @test */ + public function it_can_change_the_value_of_a_checkbox_element() + { + $this->browse(function (Browser $browser) { + $browser->visit('form/components/checkbox') + ->waitForText('FormCheckbox') + ->assertNotChecked('check_0') + ->assertChecked('check_1') + ->assertNotChecked('check_false') + ->assertChecked('check_true') + ->assertNotChecked('check_0_diff_false') + ->assertChecked('check_1_diff_false') + ->assertNotChecked('check_false_diff_false') + ->assertChecked('check_true_diff_false'); + + $checkboxes = [ + 'check_0', + 'check_1', + 'check_false', + 'check_true', + 'check_0_diff_false', + 'check_1_diff_false', + 'check_false_diff_false', + 'check_true_diff_false', + ]; + + // uncheck everything + foreach ($checkboxes as $field) { + $browser->check($field); + $browser->uncheck($field); + } + + $browser->assertSeeIn('@all', '{ "check_0": false, "check_1": false, "check_false": false, "check_true": false, "check_0_diff_false": "no", "check_1_diff_false": "no", "check_false_diff_false": "no", "check_true_diff_false": "no" }'); + + // check everything + foreach ($checkboxes as $field) { + $browser->check($field); + } + + $browser->assertSeeIn('@all', '{ "check_0": "1", "check_1": "1", "check_false": true, "check_true": true, "check_0_diff_false": "1", "check_1_diff_false": "1", "check_false_diff_false": true, "check_true_diff_false": true }'); + }); + } } diff --git a/resources/views/form/checkbox.blade.php b/resources/views/form/checkbox.blade.php index 6fd865c7..a5d105aa 100644 --- a/resources/views/form/checkbox.blade.php +++ b/resources/views/form/checkbox.blade.php @@ -8,8 +8,7 @@ 'type' => 'checkbox', 'v-model' => $vueModel(), 'data-validation-key' => $validationKey(), - ]) }} - /> + ]) }} :true-value="@js($value)" :false-value="@js($falseValue)" /> @if(trim($slot)) {{ $slot }} diff --git a/src/Components/Form/Checkbox.php b/src/Components/Form/Checkbox.php index 140ab61b..62da522d 100644 --- a/src/Components/Form/Checkbox.php +++ b/src/Components/Form/Checkbox.php @@ -17,12 +17,13 @@ class Checkbox extends Component public function __construct( public string $name = '', public string $vModel = '', - public $value = 1, + public $value = true, public string $label = '', public string $validationKey = '', public bool $showErrors = true, public string $help = '', - public bool $relation = false + public bool $relation = false, + public $falseValue = false, ) { Form::allowAttribute($name);