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

Commit 19191d2

Browse files
authored
Bugfix for setting radio elements as checked/default (#48)
* Fix for radio inputs with value "0" * Docs Co-authored-by: Pascal Baljet <[email protected]>
1 parent 5af4d67 commit 19191d2

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
All notable changes to `laravel-form-components` will be documented in this file
44

5+
## 2.5.3 - 2020-02-11
6+
7+
- Bugfix for setting radio elements as checked/default
8+
9+
## 2.5.2 - 2020-01-04
10+
11+
- Generate ID by name *and* value (checkbox and radio elements only)
12+
13+
## 2.5.1 - 2020-12-22
14+
15+
- Use the `name` attribute to auto-generate an ID (if not set)
16+
517
## 2.5.0 - 2020-12-22
618

719
- Support for `BelongsToMany`, `MorphMany`, and `MorphToMany` relationships (select element)

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,17 @@ You can group checkbox and radio elements on the same horizontal row by adding a
321321

322322
```blade
323323
<x-form-group name="notification_channel" label="How do you want to receive your notifications?" inline>
324-
<x-form-checkbox name="notification_channel" value="mail" label="Mail" />
325-
<x-form-checkbox name="notification_channel" value="slack" label="Slack" />
324+
<x-form-radio name="notification_channel" value="mail" label="Mail" />
325+
<x-form-radio name="notification_channel" value="slack" label="Slack" />
326+
</x-form-group>
327+
```
328+
329+
When you're not using target binding, you can use the `default` attribute the mark a radio element as checked:
330+
331+
```blade
332+
<x-form-group name="notification_channel" label="How do you want to receive your notifications?">
333+
<x-form-radio name="notification_channel" value="mail" label="Mail" default />
334+
<x-form-radio name="notification_channel" value="slack" label="Slack" />
326335
</x-form-group>
327336
```
328337

src/Components/FormRadio.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public function __construct(
3232
if (!session()->hasOldInput() && $this->isNotWired()) {
3333
$boundValue = $this->getBoundValue($bind, $name);
3434

35-
$this->checked = (is_null($boundValue) ? $default : $boundValue) == $this->value;
35+
if (!is_null($boundValue)) {
36+
$this->checked = $boundValue == $this->value;
37+
} else {
38+
$this->checked = $default;
39+
}
3640
}
3741
}
3842

tests/Feature/RadioTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ public function it_check_the_right_element_as_default()
1313
$this->registerTestRoute('default-radio');
1414

1515
$this->visit('/default-radio')
16+
->seeElement('input[value="1"]:checked')
17+
->seeElement('input[value="0"]:not(:checked)');
18+
}
19+
20+
/** @test */
21+
public function it_check_the_right_element_as_default_with_a_bound_target()
22+
{
23+
$this->registerTestRoute('default-radio-bind');
24+
25+
$this->visit('/default-radio-bind')
1626
->seeElement('input[value="a"]:checked')
1727
->seeElement('input[value="b"]:not(:checked)');
1828
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@php
2+
$target = ['radio' => 'a'];
3+
@endphp
4+
5+
<x-form>
6+
@bind($target)
7+
<x-form-group>
8+
<x-form-radio name="radio" value="a" />
9+
</x-form-group>
10+
11+
<x-form-group>
12+
<x-form-radio name="radio" value="b" />
13+
</x-form-group>
14+
@endbind
15+
16+
<x-form-submit />
17+
</x-form>

tests/Feature/views/default-radio.blade.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
@endphp
44

55
<x-form>
6-
@bind($target)
76
<x-form-group>
8-
<x-form-radio name="radio" value="a" />
7+
<x-form-radio name="radio" value="1" default />
98
</x-form-group>
109

1110
<x-form-group>
12-
<x-form-radio name="radio" value="b" />
11+
<x-form-radio name="radio" value="0" />
1312
</x-form-group>
14-
@endbind
1513

1614
<x-form-submit />
1715
</x-form>

0 commit comments

Comments
 (0)