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

Handling "false" values in select fields #74

Merged
merged 3 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Components/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function __construct(
if ($this->isNotWired()) {
$inputName = static::convertBracketsToDots(Str::before($name, '[]'));

$default = $this->getBoundValue($bind, $inputName) ?: $default;
if (is_null($default)) {
$default = $this->getBoundValue($bind, $inputName);
}

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

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/BindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function it_overrides_the_default_value()
$this->visit('/default-values-with-bound-target')
->seeElement('input[name="input"][value="a"]')
->seeInElement('textarea[name="textarea"]', 'b')
->seeElement('option[value="c"]:selected')
->seeElement('option[value="f"]:selected')
->seeElement('input[name="checkbox"]')
->dontSeeElement('input[name="checkbox"]:checked')
->seeElement('input[name="radio"]')
Expand All @@ -111,7 +111,7 @@ public function it_overrides_the_default_value_when_nested()
$this->visit('/default-values-with-nested-bound-target')
->seeElement('input[name="nested[input]"][value="a"]')
->seeInElement('textarea[name="nested[textarea]"]', 'b')
->seeElement('select[name="nested[select]"] > option[value="c"]:selected')
->seeElement('select[name="nested[select]"] > option[value="f"]:selected')
->seeElement('input[name="nested[checkbox]"]')
->dontSeeElement('input[name="nested[checkbox]"]:checked')
->seeElement('input[name="nested[radio]"]')
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/SelectBooleanValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ProtoneMedia\LaravelFormComponents\Tests\Feature;

use ProtoneMedia\LaravelFormComponents\Tests\TestCase;

class SelectBooleanValueTest extends TestCase
{
/** @test */
public function it_shows_the_select_field()
{
$this->registerTestRoute('select-boolean-value');

$this->visit('/select-boolean-value')
->seeElement('option[value="1"]')
->seeElement('option[value="0"]');
}

/** @test */
public function it_shows_the_false_value_selected()
{
$this->registerTestRoute('select-boolean-value');

$this->visit('/select-boolean-value')
->seeElement('option[value="1"]')
->seeElement('option[value="0"][selected="selected"]');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@bind($target)
<x-form-input default="d" name="input" />
<x-form-textarea default="e" name="textarea" />
<x-form-select default="f" name="select" :options="['' => '', 'c' => 'c']" />
<x-form-select default="f" name="select" :options="['' => '', 'c' => 'c', 'f' => 'f']" />
<x-form-checkbox :default="true" name="checkbox" />

<x-form-group name="radio">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@bind($target)
<x-form-input default="d" name="nested[input]" />
<x-form-textarea default="e" name="nested[textarea]" />
<x-form-select default="f" name="nested[select]" :options="['' => '', 'c' => 'c']" />
<x-form-select default="f" name="nested[select]" :options="['' => '', 'c' => 'c', 'f' => 'f']" />
<x-form-checkbox :default="true" name="nested[checkbox]" />

<x-form-group name="radio">
Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/views/select-boolean-value.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-form>
@bind(['select' => '0'])
<x-form-select name="select" :options="['1' => 'Yes', '0' => 'No']" />
@endbind

<x-form-submit />
</x-form>