From 396563e6e137b66c5b889e298e0d90f16b96fe2d Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 20 Oct 2024 22:02:43 +0200 Subject: [PATCH] Simplify form --- app/Mailgateway/Types/MailmanType.php | 12 ++++------ app/Mailgateway/Types/Type.php | 4 ++-- modules/Mailgateway/Components/Form.php | 24 +++++++++---------- .../Components/setting-view.blade.php | 2 +- modules/Mailgateway/UpdateTest.php | 14 +++++------ 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/app/Mailgateway/Types/MailmanType.php b/app/Mailgateway/Types/MailmanType.php index 777bfd56..449f7dfc 100644 --- a/app/Mailgateway/Types/MailmanType.php +++ b/app/Mailgateway/Types/MailmanType.php @@ -45,32 +45,28 @@ class MailmanType extends Type 'name' => 'url', 'label' => 'URL', 'type' => 'text', - 'storeValidator' => 'required|max:255', - 'updateValidator' => 'required|max:255', + 'validator' => 'required|max:255', 'default' => '', ], [ 'name' => 'user', 'label' => 'Benutzer', 'type' => 'text', - 'storeValidator' => 'required|max:255', - 'updateValidator' => 'required|max:255', + 'validator' => 'required|max:255', 'default' => '', ], [ 'name' => 'password', 'label' => 'Passwort', 'type' => 'password', - 'storeValidator' => 'required|max:255', - 'updateValidator' => 'nullable|max:255', + 'validator' => 'required|max:255', 'default' => '', ], [ 'name' => 'owner', 'label' => 'E-Mail-Adresse des Eigentümers', 'type' => 'email', - 'storeValidator' => 'required|email|max:255', - 'updateValidator' => 'required|email|max:255', + 'validator' => 'required|email|max:255', 'default' => '', ], ]; diff --git a/app/Mailgateway/Types/Type.php b/app/Mailgateway/Types/Type.php index 1b0555a0..7fd3dce0 100644 --- a/app/Mailgateway/Types/Type.php +++ b/app/Mailgateway/Types/Type.php @@ -60,10 +60,10 @@ abstract class Type /** * @return array */ - public static function rules(string $validator): array + public static function rules(): array { return collect(static::fields())->mapWithKeys(fn ($field) => [ - $field['name'] => $field[$validator], + $field['name'] => $field['validator'], ])->toArray(); } diff --git a/modules/Mailgateway/Components/Form.php b/modules/Mailgateway/Components/Form.php index 1dda7427..c24b3ff7 100644 --- a/modules/Mailgateway/Components/Form.php +++ b/modules/Mailgateway/Components/Form.php @@ -13,13 +13,13 @@ use Livewire\Component; class Form extends Component { - public string $id = ''; public string $name = ''; public string $domain = ''; public array $params = []; #[Validate('required')] public ?string $cls = null; public Collection $types; + public ?Mailgateway $model = null; public function rules() { @@ -28,7 +28,7 @@ class Form extends Component 'domain' => 'required|string|max:255', 'cls' => ['required', 'string', 'max:255', Rule::in(app('mail-gateways'))], 'params' => 'present|array', - ...$this->cls ? collect($this->cls::rules($this->id ? 'updateValidator' : 'storeValidator'))->mapWithKeys(fn ($rules, $key) => ["params.{$key}" => $rules]) : [], + ...$this->cls ? collect($this->cls::rules())->mapWithKeys(fn ($rules, $key) => ["params.{$key}" => $rules]) : [], ]; } @@ -42,19 +42,19 @@ class Form extends Component ]; } - public function mount(?Mailgateway $model = null): void + public function mount(?string $id = null): void { $this->types = app('mail-gateways')->map(fn ($gateway) => [ 'name' => $gateway::name(), 'id' => $gateway, ]); - if ($model->getRouteKey()) { - $this->id = $model->id; - $this->name = $model->name; - $this->domain = $model->domain; - $this->cls = get_class($model->type); - $this->params = (array) $model->type; + if ($id) { + $this->model = Mailgateway::find($id); + $this->name = $this->model->name; + $this->domain = $this->model->domain; + $this->cls = get_class($this->model->type); + $this->params = (array) $this->model->type; } } @@ -82,8 +82,8 @@ class Form extends Component 'domain' => $this->domain, 'type' => ['cls' => $this->cls, 'params' => $this->params], ]; - if ($this->id) { - Mailgateway::find($this->id)->update($payload); + if ($this->model) { + $this->model->update($payload); } else { Mailgateway::create($payload); } @@ -107,7 +107,7 @@ class Form extends Component :label="$field['label']" :type="$field['type']" :name="$field['name']" - :required="str_contains('required', $field['storeValidator'])" + :required="str_contains('required', $field['validator'])" > @endforeach diff --git a/modules/Mailgateway/Components/setting-view.blade.php b/modules/Mailgateway/Components/setting-view.blade.php index 3dfcd6cd..bb3473e7 100644 --- a/modules/Mailgateway/Components/setting-view.blade.php +++ b/modules/Mailgateway/Components/setting-view.blade.php @@ -26,7 +26,7 @@ Bearbeiten diff --git a/modules/Mailgateway/UpdateTest.php b/modules/Mailgateway/UpdateTest.php index e84cb2d2..dd4e3907 100644 --- a/modules/Mailgateway/UpdateTest.php +++ b/modules/Mailgateway/UpdateTest.php @@ -20,8 +20,8 @@ it('test it sets attributes for mailman', function () { $typeParams = MailmanTypeRequest::new()->create(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner']); $mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create(['name' => '::name::', 'domain' => 'example.com']); - Livewire::test(Form::class, ['model' => $mailgateway->id]) - ->assertSet('id', $mailgateway->id) + Livewire::test(Form::class, ['id' => $mailgateway->id]) + ->assertSet('model', fn ($m) => $m->is($mailgateway)) ->assertSet('name', '::name::') ->assertSet('domain', 'example.com') ->assertSet('cls', MailmanType::class) @@ -36,7 +36,7 @@ it('test it sets attributes for local', function () { $mailgateway = Mailgateway::factory()->type(LocalType::class, [])->create(['name' => '::name::', 'domain' => 'example.com']); - Livewire::test(Form::class, ['model' => $mailgateway->id]) + Livewire::test(Form::class, ['id' => $mailgateway->id]) ->assertSet('name', '::name::') ->assertSet('domain', 'example.com') ->assertSet('cls', LocalType::class) @@ -48,7 +48,7 @@ it('test it validates type', function () { $mailgateway = Mailgateway::factory()->type(LocalType::class, [])->create(['name' => '::name::', 'domain' => 'example.com']); - Livewire::test(Form::class, ['model' => $mailgateway->id]) + Livewire::test(Form::class, ['id' => $mailgateway->id]) ->set('cls', '') ->assertHasErrors(['cls' => 'required']); }); @@ -59,7 +59,7 @@ it('test it updates a mailman gateway without updating password', function () { $typeParams = MailmanTypeRequest::new()->succeeds()->create(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com']); $mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create(['name' => '::name::', 'domain' => 'example.com']); - Livewire::test(Form::class, ['model' => $mailgateway->id]) + Livewire::test(Form::class, ['id' => $mailgateway->id]) ->set('name', '::newname::') ->call('onSave') ->assertHasNoErrors() @@ -81,7 +81,7 @@ it('test it updates a mailman gateway with password', function () { $newTypeParams = MailmanTypeRequest::new()->succeeds()->create(['url' => 'https://mailman.example.com', 'user' => 'newuser', 'password' => 'password', 'owner' => 'owner@example.com']); $mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create(); - Livewire::test(Form::class, ['model' => $mailgateway->id]) + Livewire::test(Form::class, ['id' => $mailgateway->id]) ->set('params.user', 'newuser') ->call('onSave') ->assertHasNoErrors(); @@ -99,7 +99,7 @@ it('test it checks mailgateway connection when updating', function () { MailmanTypeRequest::new()->fails()->create(['url' => 'https://mailman.example.com', 'user' => 'newuser', 'password' => 'password', 'owner' => 'owner@example.com']); $mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create(); - Livewire::test(Form::class, ['model' => $mailgateway->id]) + Livewire::test(Form::class, ['id' => $mailgateway->id]) ->set('params.user', 'newuser') ->call('onSave') ->assertHasErrors('connection');