Skip to content

Commit

Permalink
Add form translation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
francoism90 committed Oct 11, 2024
1 parent d25623e commit 81c5dbe
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions src/App/Web/Shared/Concerns/WithFormTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Web\Shared\Concerns;

use Illuminate\Database\Eloquent\Model;

trait WithFormTranslations
{
protected function setTranslations(): void
{
collect($this->all())
->filter(fn (mixed $value) => is_string($value) && filled($value))
->each(fn (?string $value, string $key) => data_set($this, $key, str($value ?: '')->squish()->value()));
}

protected function getModelTranslations(Model $model, ?string $locale = null): array
{
$locale ??= app()->getLocale();

return collect($model->getTranslations())
->map(fn (?array $item) => data_get($item, $locale))
->toArray();
}
}
5 changes: 5 additions & 0 deletions src/App/Web/Tags/Controllers/TagEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ protected function authorizeAccess(): void
$this->canUpdate($this->tag);
}

public function updatedForm(): void
{
$this->form->validate();
}

public function submit(): void
{
$this->authorize('update', $model = $this->tag);
Expand Down
19 changes: 9 additions & 10 deletions src/App/Web/Tags/Forms/GeneralForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Web\Tags\Forms;

use App\Web\Shared\Concerns\WithFormTranslations;
use Domain\Tags\Enums\TagType;
use Domain\Tags\Models\Tag;
use Foxws\WireUse\Forms\Support\Form;
Expand All @@ -10,6 +11,8 @@

class GeneralForm extends Form
{
use WithFormTranslations;

#[Validate]
public string $name = '';

Expand All @@ -22,13 +25,6 @@ class GeneralForm extends Form
#[Validate(['related' => 'nullable|array', 'related.*.id' => 'exists:tags,prefixed_id'])]
public array $related = [];

protected function beforeValidate(): void
{
collect($this->all())
->filter(fn (mixed $value) => filled($value) && is_string($value))
->each(fn (mixed $value, string $key) => data_set($this, $key, str($value)->squish()->value()));
}

public function rules(): array
{
return [
Expand All @@ -38,11 +34,14 @@ public function rules(): array
];
}

protected function beforeValidate(): void
{
$this->setTranslations();
}

protected function beforeFill(Tag $model): array
{
$translations = collect($model->getTranslations())
->map(fn (?array $item) => data_get($item, app()->getLocale(), ''))
->toArray();
$translations = $this->getModelTranslations($model);

$values = [...$model->toArray(), ...$translations];

Expand Down
5 changes: 5 additions & 0 deletions src/App/Web/Videos/Controllers/VideoEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ protected function authorizeAccess(): void
$this->canUpdate($this->video);
}

public function updatedForm(): void
{
$this->form->validate();
}

public function submit(): void
{
$this->authorize('update', $model = $this->video);
Expand Down
16 changes: 7 additions & 9 deletions src/App/Web/Videos/Forms/GeneralForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace App\Web\Videos\Forms;

use App\Web\Shared\Concerns\WithFormTranslations;
use Domain\Videos\Models\Video;
use Foxws\WireUse\Forms\Support\Form;
use Livewire\Attributes\Validate;

class GeneralForm extends Form
{
use WithFormTranslations;

#[Validate('required|string|min:1|max:255')]
public string $name = '';
public ?string $name = null;

#[Validate('nullable|string|min:1|max:255')]
public ?string $episode = null;
Expand All @@ -30,24 +33,19 @@ class GeneralForm extends Form
public ?string $released_at = null;

#[Validate(['tags' => 'nullable|array', 'tags.*.id' => 'exists:tags,prefixed_id'])]
public array $tags = [];
public ?array $tags = [];

protected function beforeValidate(): void
{
collect($this->all())
->filter(fn (mixed $value) => is_string($value) && filled($value))
->each(fn (mixed $value, string $key) => data_set($this, $key, str($value)->squish()->value()));
$this->setTranslations();
}

protected function beforeFill(Video $model): array
{
$translations = collect($model->getTranslations())
->map(fn (?array $item) => data_get($item, app()->getLocale(), ''))
->toArray();
$translations = $this->getModelTranslations($model);

$values = [...$model->toArray(), ...$translations];

// Convert tags to options
$values['tags'] = $model->tags->options()->toArray();

return $values;
Expand Down

0 comments on commit 81c5dbe

Please sign in to comment.