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

Commit b44bce8

Browse files
authored
Translation with custom bind bugfix (#5)
1 parent f7c12a4 commit b44bce8

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/Components/HandlesDefaultAndOldValue.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ private function setValue(
2222
return $this->value = old($name, $default);
2323
}
2424

25-
$bind = $this->getBoundTarget($bind, $name);
25+
if ($bind !== false) {
26+
$bind = $bind ?: $this->getBoundTarget();
27+
}
2628

2729
if ($bind) {
2830
$default = $bind->getTranslation($name, $language, false) ?: $default;

tests/Feature/TranslatableModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class TranslatableModel extends Model
99
{
1010
use HasTranslations;
1111

12-
public $translatable = ['input'];
12+
public $translatable = ['input', 'output'];
1313
}

tests/Feature/TranslationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public function it_can_bind_a_target_to_the_form()
2727
->seeElement('input[name="input[en]"][value="hello"]');
2828
}
2929

30+
/** @test */
31+
public function it_can_override_the_bind_with_a_different_target()
32+
{
33+
$this->registerTestRoute('translation-with-bind');
34+
35+
$this->visit('/translation-with-bind')
36+
->seeElement('input[name="output[nl]"][value="vaarwel"]')
37+
->seeElement('input[name="output[en]"][value="goodbye"]');
38+
}
39+
3040
/** @test */
3141
public function it_shows_the_validation_errors_and_old_values_correctly()
3242
{

tests/Feature/views/translation-with-bind.blade.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
@php
2-
$target = new \ProtoneMedia\LaravelFormComponents\Tests\Feature\TranslatableModel;
3-
$target->setTranslations('input', ['nl' => 'hallo', 'en' => 'hello']);
2+
$targetA = new \ProtoneMedia\LaravelFormComponents\Tests\Feature\TranslatableModel;
3+
$targetA->setTranslations('input', ['nl' => 'hallo', 'en' => 'hello']);
4+
5+
$targetB = new \ProtoneMedia\LaravelFormComponents\Tests\Feature\TranslatableModel;
6+
$targetB->setTranslations('output', ['nl' => 'vaarwel', 'en' => 'goodbye']);
47
@endphp
58

69
<x-form>
7-
@bind($target)
10+
@bind($targetA)
811
<x-form-input name="input" language="nl" />
12+
<x-form-input name="output" language="nl" :bind="$targetB" />
13+
914
<x-form-input name="input" language="en" />
15+
<x-form-input name="output" language="en" :bind="$targetB" />
1016

1117
<x-form-submit />
1218
@endbind

0 commit comments

Comments
 (0)