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

Commit 8373ac2

Browse files
Handling "false" values in select fields (#74)
Co-authored-by: Pascal Baljet <[email protected]>
1 parent dac240b commit 8373ac2

6 files changed

+42
-5
lines changed

src/Components/FormSelect.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public function __construct(
4545
if ($this->isNotWired()) {
4646
$inputName = static::convertBracketsToDots(Str::before($name, '[]'));
4747

48-
$default = $this->getBoundValue($bind, $inputName) ?: $default;
48+
if (is_null($default)) {
49+
$default = $this->getBoundValue($bind, $inputName);
50+
}
4951

5052
$this->selectedKey = old($inputName, $default);
5153

tests/Feature/BindTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function it_overrides_the_default_value()
9696
$this->visit('/default-values-with-bound-target')
9797
->seeElement('input[name="input"][value="a"]')
9898
->seeInElement('textarea[name="textarea"]', 'b')
99-
->seeElement('option[value="c"]:selected')
99+
->seeElement('option[value="f"]:selected')
100100
->seeElement('input[name="checkbox"]')
101101
->dontSeeElement('input[name="checkbox"]:checked')
102102
->seeElement('input[name="radio"]')
@@ -111,7 +111,7 @@ public function it_overrides_the_default_value_when_nested()
111111
$this->visit('/default-values-with-nested-bound-target')
112112
->seeElement('input[name="nested[input]"][value="a"]')
113113
->seeInElement('textarea[name="nested[textarea]"]', 'b')
114-
->seeElement('select[name="nested[select]"] > option[value="c"]:selected')
114+
->seeElement('select[name="nested[select]"] > option[value="f"]:selected')
115115
->seeElement('input[name="nested[checkbox]"]')
116116
->dontSeeElement('input[name="nested[checkbox]"]:checked')
117117
->seeElement('input[name="nested[radio]"]')
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelFormComponents\Tests\Feature;
4+
5+
use ProtoneMedia\LaravelFormComponents\Tests\TestCase;
6+
7+
class SelectBooleanValueTest extends TestCase
8+
{
9+
/** @test */
10+
public function it_shows_the_select_field()
11+
{
12+
$this->registerTestRoute('select-boolean-value');
13+
14+
$this->visit('/select-boolean-value')
15+
->seeElement('option[value="1"]')
16+
->seeElement('option[value="0"]');
17+
}
18+
19+
/** @test */
20+
public function it_shows_the_false_value_selected()
21+
{
22+
$this->registerTestRoute('select-boolean-value');
23+
24+
$this->visit('/select-boolean-value')
25+
->seeElement('option[value="1"]')
26+
->seeElement('option[value="0"][selected="selected"]');
27+
}
28+
}

tests/Feature/views/default-values-with-bound-target.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@bind($target)
1313
<x-form-input default="d" name="input" />
1414
<x-form-textarea default="e" name="textarea" />
15-
<x-form-select default="f" name="select" :options="['' => '', 'c' => 'c']" />
15+
<x-form-select default="f" name="select" :options="['' => '', 'c' => 'c', 'f' => 'f']" />
1616
<x-form-checkbox :default="true" name="checkbox" />
1717

1818
<x-form-group name="radio">

tests/Feature/views/default-values-with-nested-bound-target.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@bind($target)
1515
<x-form-input default="d" name="nested[input]" />
1616
<x-form-textarea default="e" name="nested[textarea]" />
17-
<x-form-select default="f" name="nested[select]" :options="['' => '', 'c' => 'c']" />
17+
<x-form-select default="f" name="nested[select]" :options="['' => '', 'c' => 'c', 'f' => 'f']" />
1818
<x-form-checkbox :default="true" name="nested[checkbox]" />
1919

2020
<x-form-group name="radio">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<x-form>
2+
@bind(['select' => '0'])
3+
<x-form-select name="select" :options="['1' => 'Yes', '0' => 'No']" />
4+
@endbind
5+
6+
<x-form-submit />
7+
</x-form>

0 commit comments

Comments
 (0)