This repository was archived by the owner on Jan 2, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +86
-3
lines changed Expand file tree Collapse file tree 5 files changed +86
-3
lines changed Original file line number Diff line number Diff line change 2
2
3
3
All notable changes to ` laravel-form-components ` will be documented in this file
4
4
5
+ ## 2.1.2 - 2020-09-29
6
+
7
+ - Bugfix for multiple checkbox/radio elements with the same name
8
+
9
+ ## 2.1.1 - 2020-09-29
10
+
11
+ - Bugfix for select options with numeric keys
12
+
5
13
## 2.1.0 - 2020-09-14
6
14
7
15
- Select elements now support a slot
Original file line number Diff line number Diff line change 2
2
3
3
namespace ProtoneMedia \LaravelFormComponents \Components ;
4
4
5
+ use Illuminate \Support \Arr ;
6
+ use Illuminate \Support \Str ;
7
+
5
8
class FormCheckbox extends Component
6
9
{
7
10
use HandlesValidationErrors;
@@ -30,12 +33,19 @@ public function __construct(
30
33
$ this ->value = $ value ;
31
34
$ this ->showErrors = $ showErrors ;
32
35
33
- if (old ($ name )) {
34
- $ this ->checked = true ;
36
+ $ inputName = Str::before ($ name , '[] ' );
37
+
38
+ if ($ oldData = old ($ inputName )) {
39
+ $ this ->checked = in_array ($ value , Arr::wrap ($ oldData ));
35
40
}
36
41
37
42
if (!session ()->hasOldInput () && $ this ->isNotWired ()) {
38
- $ boundValue = $ this ->getBoundValue ($ bind , $ name );
43
+ $ boundValue = $ this ->getBoundValue ($ bind , $ inputName );
44
+
45
+ if (is_array ($ boundValue )) {
46
+ $ this ->checked = in_array ($ value , $ boundValue );
47
+ return ;
48
+ }
39
49
40
50
$ this ->checked = is_null ($ boundValue ) ? $ default : $ boundValue ;
41
51
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ProtoneMedia \LaravelFormComponents \Tests \Feature ;
4
+
5
+ use Illuminate \Http \Request ;
6
+ use ProtoneMedia \LaravelFormComponents \Tests \TestCase ;
7
+
8
+ class ChekboxTest extends TestCase
9
+ {
10
+ /** @test */
11
+ public function it_check_the_right_element_as_default ()
12
+ {
13
+ $ this ->registerTestRoute ('default-checkbox ' );
14
+
15
+ $ this ->visit ('/default-checkbox ' )
16
+ ->seeElement ('input[value="a"]:checked ' )
17
+ ->seeElement ('input[value="b"]:not(:checked) ' );
18
+ }
19
+
20
+ /** @test */
21
+ public function it_does_check_the_right_input_element_after_a_validation_error ()
22
+ {
23
+ $ this ->registerTestRoute ('checkbox-validation ' , function (Request $ request ) {
24
+ $ data = $ request ->validate ([
25
+ 'checkbox ' => 'required|array ' ,
26
+ 'checkbox.* ' => 'in:a ' ,
27
+ ]);
28
+ });
29
+
30
+ $ this ->visit ('/checkbox-validation?check=b ' )
31
+ ->seeElement ('input[value="a"]:not(:checked) ' )
32
+ ->seeElement ('input[value="b"]:checked ' )
33
+ ->press ('Submit ' )
34
+ ->seeElement ('input[value="a"]:not(:checked) ' )
35
+ ->seeElement ('input[value="b"]:checked ' );
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ <x-form action =" /checkbox-validation" >
2
+ <x-form-group >
3
+ <x-form-checkbox name =" checkbox[]" value =" a" :default =" request()->query('check') == 'a'" />
4
+ </x-form-group >
5
+
6
+ <x-form-group >
7
+ <x-form-checkbox name =" checkbox[]" value =" b" :default =" request()->query('check') == 'b'" />
8
+ </x-form-group >
9
+
10
+ <x-form-submit />
11
+ </x-form >
Original file line number Diff line number Diff line change
1
+ @php
2
+ $target = [' checkbox' => [' a' ]];
3
+ @endphp
4
+
5
+ <x-form >
6
+ @bind ($target )
7
+ <x-form-group >
8
+ <x-form-checkbox name =" checkbox[]" value =" a" />
9
+ </x-form-group >
10
+
11
+ <x-form-group >
12
+ <x-form-checkbox name =" checkbox[]" value =" b" />
13
+ </x-form-group >
14
+ @endbind
15
+
16
+ <x-form-submit />
17
+ </x-form >
You can’t perform that action at this time.
0 commit comments