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

Commit 9b0717b

Browse files
committed
Better use of $attributes and minor fixes
1 parent 64ffe1d commit 9b0717b

16 files changed

+72
-43
lines changed

CHANGELOG.md

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

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

5+
## 1.0.1 - 2020-07-18
6+
7+
- added `hasErrorAndShow` method to components
8+
- use `$slot` and added `$attributes` to `form-button`
9+
- added `$attibutes` to `form-group` and `form-label`
10+
- fixed translation bug
11+
512
## 1.0.0 - 2020-07-17
613

714
- initial release

resources/views/tailwind/form-checkbox.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<span class="ml-2">{{ $label }}</span>
1414
</label>
1515

16-
@if($showErrors)
16+
@if($hasErrorAndShow($name))
1717
<x-form-errors :name="$name" />
1818
@endif
1919
</div>
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
@if($name)
2-
@error($name)
3-
<p {!! $attributes->merge(['class' => 'text-red-500 text-xs italic']) !!}>
4-
{{ $message }}
5-
</p>
6-
@enderror
7-
@endif
1+
@error($name)
2+
<p {!! $attributes->merge(['class' => 'text-red-500 text-xs italic']) !!}>
3+
{{ $message }}
4+
</p>
5+
@enderror
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<div class="mt-4">
1+
<div {!! $attributes->merge(['class' => 'mt-4']) !!}>
22
<x-form-label :label="$label" />
33

44
<div class="@if($label) mt-2 @endif @if($inline) flex flex-wrap space-x-6 @endif">
55
{!! $slot !!}
66
</div>
77

8-
@if($showErrors)
8+
@if($hasErrorAndShow($name))
99
<x-form-errors :name="$name" />
1010
@endif
1111
</div>

resources/views/tailwind/form-input.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/>
1212
</label>
1313

14-
@if($showErrors)
14+
@if($hasErrorAndShow($name))
1515
<x-form-errors :name="$name" />
1616
@endif
1717
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@if($label)
2-
<span class="text-gray-700">{{ $label }}</span>
2+
<span {!! $attributes->merge(['class' => 'text-gray-700']) !!}>{{ $label }}</span>
33
@endif

resources/views/tailwind/form-radio.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<span class="ml-2">{{ $label }}</span>
1414
</label>
1515

16-
@if($showErrors)
16+
@if($hasErrorAndShow($name))
1717
<x-form-errors :name="$name" />
1818
@endif
1919
</div>

resources/views/tailwind/form-select.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</select>
1919
</label>
2020

21-
@if($showErrors)
21+
@if($hasErrorAndShow($name))
2222
<x-form-errors :name="$name" />
2323
@endif
2424
</div>
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<div class="mt-6 flex items-center justify-between">
2-
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="submit">
3-
{{ $label }}
2+
<button {!! $attributes->merge([
3+
'class' => 'bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline',
4+
'type' => 'submit'
5+
]) !!}>
6+
{!! trim($slot) ?: __('Submit') !!}
47
</button>
58
</div>

resources/views/tailwind/form-textarea.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
name="{{ $name }}">{!! $value !!}</textarea>
99
</label>
1010

11-
@if($showErrors)
11+
@if($hasErrorAndShow($name))
1212
<x-form-errors :name="$name" />
1313
@endif
1414
</div>

src/Components/FormSubmit.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,4 @@
44

55
class FormSubmit extends Component
66
{
7-
public string $label;
8-
9-
/**
10-
* Create a new component instance.
11-
*
12-
* @return void
13-
*/
14-
public function __construct(string $label = '')
15-
{
16-
$this->label = $label ?: __('Submit');
17-
}
187
}

src/Components/HandlesDefaultAndOldValue.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ private function setValue(
2020

2121
$bind = $this->getBoundTarget($bind, $name);
2222

23-
$default = $bind->getTranslation($name, $language, false) ?: $default;
23+
if ($bind) {
24+
$default = $bind->getTranslation($name, $language, false) ?: $default;
25+
}
2426

2527
$this->value = old("{$name}.{$language}", $default);
2628
}

src/Components/HandlesValidationErrors.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22

33
namespace ProtoneMedia\LaravelFormComponents\Components;
44

5+
use Illuminate\Support\ViewErrorBag;
6+
57
trait HandlesValidationErrors
68
{
79
public $showErrors = true;
10+
11+
public function hasErrorAndShow(string $name, string $bag = 'default'): bool
12+
{
13+
if (!$this->showErrors) {
14+
return false;
15+
}
16+
17+
$errors = request()->session()->get('errors') ?: new ViewErrorBag;
18+
19+
$name = str_replace(['[', ']'], ['.', ''], $name);
20+
21+
return $errors->getBag($bag)->has($name);
22+
}
823
}

tests/Feature/TranslationTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,35 @@
88
class TranslationTest extends TestCase
99
{
1010
/** @test */
11-
public function it_can_bind_a_target_to_the_form()
11+
public function it_can_have_no_target_bound_to_the_form()
1212
{
1313
$this->registerTestRoute('translation');
1414

1515
$this->visit('/translation')
16+
->seeElement('input[name="input[nl]"][value=""]')
17+
->seeElement('input[name="input[en]"][value=""]');
18+
}
19+
20+
/** @test */
21+
public function it_can_bind_a_target_to_the_form()
22+
{
23+
$this->registerTestRoute('translation-with-bind');
24+
25+
$this->visit('/translation-with-bind')
1626
->seeElement('input[name="input[nl]"][value="hallo"]')
1727
->seeElement('input[name="input[en]"][value="hello"]');
1828
}
1929

2030
/** @test */
2131
public function it_shows_the_validation_errors_and_old_values_correctly()
2232
{
23-
$this->registerTestRoute('translation', function (Request $request) {
33+
$this->registerTestRoute('translation-with-bind', function (Request $request) {
2434
$request->validate([
2535
'input.*' => 'min:5',
2636
]);
2737
});
2838

29-
$this->visit('/translation')
39+
$this->visit('/translation-with-bind')
3040
->type('hoi', 'input[nl]')
3141
->type('hey', 'input[en]')
3242
->press('Submit')
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@php
2+
$target = new \ProtoneMedia\LaravelFormComponents\Tests\Feature\TranslatableModel;
3+
$target->setTranslations('input', ['nl' => 'hallo', 'en' => 'hello']);
4+
@endphp
5+
6+
<x-form>
7+
@bind($target)
8+
<x-form-input name="input" language="nl" />
9+
<x-form-input name="input" language="en" />
10+
11+
<x-form-submit />
12+
@endbind
13+
</x-form>
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
@php
2-
$target = new \ProtoneMedia\LaravelFormComponents\Tests\Feature\TranslatableModel;
3-
$target->setTranslations('input', ['nl' => 'hallo', 'en' => 'hello']);
4-
@endphp
5-
61
<x-form>
7-
@bind($target)
8-
<x-form-input name="input" language="nl" />
9-
<x-form-input name="input" language="en" />
10-
11-
<x-form-submit />
12-
@endbind
2+
<x-form-input name="input" language="nl" />
3+
<x-form-input name="input" language="en" />
4+
<x-form-submit />
135
</x-form>

0 commit comments

Comments
 (0)