Skip to content

Commit 1e3d7ff

Browse files
committed
Fix: direct value has precedence over model binding
1 parent 3d3271d commit 1e3d7ff

File tree

15 files changed

+107
-18
lines changed

15 files changed

+107
-18
lines changed

src/Components/BaseFormInput.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ public function __construct(string $name, ?string $oldReference = null, ?string
2828
$this->modelField = $modelField ?: $name;
2929

3030
$valueToLoad = $this->loadFromOld();
31+
$override = true;
3132

3233
if ($valueToLoad === null) {
3334
$valueToLoad = $this->loadFromModel();
35+
$override = false;
3436
}
3537

3638
if ($valueToLoad !== null) {
3739
try {
38-
$this->load($valueToLoad);
40+
$this->load($valueToLoad, $override);
3941

4042
} catch (\TypeError $e) {
4143
// Wrong variable type
@@ -46,7 +48,7 @@ public function __construct(string $name, ?string $oldReference = null, ?string
4648
/**
4749
* Method called when a value is found, either from the binded model or the `old()` input
4850
*/
49-
abstract public function load(mixed $value): void;
51+
abstract public function load(mixed $value, bool $override): void;
5052

5153
protected function loadFromModel(): mixed
5254
{

src/Components/Checkbox.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ public function __construct(
2828
parent::__construct($name);
2929
}
3030

31-
public function load(mixed $value): void
31+
public function load(mixed $value, bool $override): void
3232
{
33+
if (!$override && $this->checked !== null) {
34+
return;
35+
}
36+
3337
$this->checked = boolval($value);
3438
}
3539

src/Components/Input.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ public function __construct(
2525
parent::__construct($name);
2626
}
2727

28-
public function load(mixed $value): void
28+
public function load(mixed $value, bool $override): void
2929
{
30+
if (!$override && $this->value !== null) {
31+
return;
32+
}
33+
3034
$this->value = $this->parse($value);
3135
}
3236

src/Components/MultiSelect.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,22 @@ public function __construct(
3737

3838
$this->fieldName = "{$name}[]";
3939

40-
$this->load($value);
40+
$this->load($value, false);
4141

4242
parent::__construct($name);
4343

4444
}
4545

46-
public function load(mixed $value): void
46+
public function load(mixed $value, bool $override): void
4747
{
48+
if (!$override && $this->value) {
49+
return;
50+
}
51+
4852
if (is_a($value, Collection::class)) {
4953
$value = $value->toArray();
5054
}
55+
5156
$this->value = $value ?: [];
5257
}
5358

src/Components/Radio.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ public function __construct(
2020
parent::__construct($name);
2121
}
2222

23-
public function load(mixed $value): void
23+
public function load(mixed $value, bool $override): void
2424
{
25+
if (!$override && $this->checked !== null) {
26+
return;
27+
}
28+
2529
$value = EnumConverter::enumToValue($value);
2630

2731
$this->checked = $value === $this->value;

src/Components/SearchSelect.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ public function __construct(
3737

3838
}
3939

40-
public function load(mixed $value): void
40+
public function load(mixed $value, bool $override): void
4141
{
42+
if (!$override && $this->value !== null) {
43+
return;
44+
}
45+
4246
$this->value = $this->parse($value);
4347
}
4448

src/Components/Select.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ public function __construct(
3333

3434
}
3535

36-
public function load(mixed $value): void
36+
public function load(mixed $value, bool $override): void
3737
{
38+
if (!$override && $this->value !== null) {
39+
return;
40+
}
41+
3842
$this->value = $this->parse($value);
3943
}
4044

src/Components/Textarea.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ public function __construct(
2525

2626
}
2727

28-
public function load(mixed $value): void
28+
public function load(mixed $value, bool $override): void
2929
{
30+
if (!$override && $this->value !== null) {
31+
return;
32+
}
33+
3034
$this->value = $this->parse($value);
3135
}
3236

tests/Feature/Components/CheckboxTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
bindModel($testModel);
3333

34-
$checkbox = new Checkbox(name: 'enabled', checked: false);
34+
$checkbox = new Checkbox(name: 'enabled');
3535

3636
expect($checkbox->checked)->toBeTrue();
3737
});
@@ -96,7 +96,6 @@
9696

9797
$view = $this->blade(
9898
'<x-bs::checkbox name="enabled" />',
99-
['checked' => true]
10099
);
101100

102101
expect($view)->assertSee('checked');

tests/Feature/Components/InputTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
$testModel = new TestModel(['email' => 'test_model@example.com']);
3939
bindModel($testModel);
4040

41-
$input = new Input(name: 'email', value: 'test@example.com');
41+
$input = new Input(name: 'email');
4242

4343
expect($input->value)->toBe('test_model@example.com');
4444
});
@@ -57,6 +57,16 @@
5757
expect($input->value)->toBe('test_old@example.com');
5858
});
5959

60+
it('gives priority to value', function () {
61+
62+
$testModel = new TestModel(['email' => 'test_model@example.com']);
63+
bindModel($testModel);
64+
65+
$input = new Input(name: 'email', value: 'test@example.com');
66+
67+
expect($input->value)->toBe('test@example.com');
68+
});
69+
6070
});
6171

6272
describe('Renders correctly', function () {

0 commit comments

Comments
 (0)