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 22
33All notable changes to ` laravel-form-components ` will be documented in this file
44
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+
513## 2.1.0 - 2020-09-14
614
715- Select elements now support a slot
Original file line number Diff line number Diff line change 22
33namespace ProtoneMedia \LaravelFormComponents \Components ;
44
5+ use Illuminate \Support \Arr ;
6+ use Illuminate \Support \Str ;
7+
58class FormCheckbox extends Component
69{
710 use HandlesValidationErrors;
@@ -30,12 +33,19 @@ public function __construct(
3033 $ this ->value = $ value ;
3134 $ this ->showErrors = $ showErrors ;
3235
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 ));
3540 }
3641
3742 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+ }
3949
4050 $ this ->checked = is_null ($ boundValue ) ? $ default : $ boundValue ;
4151 }
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