Simplify form
This commit is contained in:
parent
19a13df20d
commit
c5bb2e3428
|
@ -45,32 +45,28 @@ class MailmanType extends Type
|
||||||
'name' => 'url',
|
'name' => 'url',
|
||||||
'label' => 'URL',
|
'label' => 'URL',
|
||||||
'type' => 'text',
|
'type' => 'text',
|
||||||
'storeValidator' => 'required|max:255',
|
'validator' => 'required|max:255',
|
||||||
'updateValidator' => 'required|max:255',
|
|
||||||
'default' => '',
|
'default' => '',
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'user',
|
'name' => 'user',
|
||||||
'label' => 'Benutzer',
|
'label' => 'Benutzer',
|
||||||
'type' => 'text',
|
'type' => 'text',
|
||||||
'storeValidator' => 'required|max:255',
|
'validator' => 'required|max:255',
|
||||||
'updateValidator' => 'required|max:255',
|
|
||||||
'default' => '',
|
'default' => '',
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'password',
|
'name' => 'password',
|
||||||
'label' => 'Passwort',
|
'label' => 'Passwort',
|
||||||
'type' => 'password',
|
'type' => 'password',
|
||||||
'storeValidator' => 'required|max:255',
|
'validator' => 'required|max:255',
|
||||||
'updateValidator' => 'nullable|max:255',
|
|
||||||
'default' => '',
|
'default' => '',
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'owner',
|
'name' => 'owner',
|
||||||
'label' => 'E-Mail-Adresse des Eigentümers',
|
'label' => 'E-Mail-Adresse des Eigentümers',
|
||||||
'type' => 'email',
|
'type' => 'email',
|
||||||
'storeValidator' => 'required|email|max:255',
|
'validator' => 'required|email|max:255',
|
||||||
'updateValidator' => 'required|email|max:255',
|
|
||||||
'default' => '',
|
'default' => '',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
|
@ -60,10 +60,10 @@ abstract class Type
|
||||||
/**
|
/**
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public static function rules(string $validator): array
|
public static function rules(): array
|
||||||
{
|
{
|
||||||
return collect(static::fields())->mapWithKeys(fn ($field) => [
|
return collect(static::fields())->mapWithKeys(fn ($field) => [
|
||||||
$field['name'] => $field[$validator],
|
$field['name'] => $field['validator'],
|
||||||
])->toArray();
|
])->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,13 +13,13 @@ use Livewire\Component;
|
||||||
class Form extends Component
|
class Form extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
public string $id = '';
|
|
||||||
public string $name = '';
|
public string $name = '';
|
||||||
public string $domain = '';
|
public string $domain = '';
|
||||||
public array $params = [];
|
public array $params = [];
|
||||||
#[Validate('required')]
|
#[Validate('required')]
|
||||||
public ?string $cls = null;
|
public ?string $cls = null;
|
||||||
public Collection $types;
|
public Collection $types;
|
||||||
|
public ?Mailgateway $model = null;
|
||||||
|
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
|
@ -28,7 +28,7 @@ class Form extends Component
|
||||||
'domain' => 'required|string|max:255',
|
'domain' => 'required|string|max:255',
|
||||||
'cls' => ['required', 'string', 'max:255', Rule::in(app('mail-gateways'))],
|
'cls' => ['required', 'string', 'max:255', Rule::in(app('mail-gateways'))],
|
||||||
'params' => 'present|array',
|
'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) => [
|
$this->types = app('mail-gateways')->map(fn ($gateway) => [
|
||||||
'name' => $gateway::name(),
|
'name' => $gateway::name(),
|
||||||
'id' => $gateway,
|
'id' => $gateway,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($model->getRouteKey()) {
|
if ($id) {
|
||||||
$this->id = $model->id;
|
$this->model = Mailgateway::find($id);
|
||||||
$this->name = $model->name;
|
$this->name = $this->model->name;
|
||||||
$this->domain = $model->domain;
|
$this->domain = $this->model->domain;
|
||||||
$this->cls = get_class($model->type);
|
$this->cls = get_class($this->model->type);
|
||||||
$this->params = (array) $model->type;
|
$this->params = (array) $this->model->type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,8 +82,8 @@ class Form extends Component
|
||||||
'domain' => $this->domain,
|
'domain' => $this->domain,
|
||||||
'type' => ['cls' => $this->cls, 'params' => $this->params],
|
'type' => ['cls' => $this->cls, 'params' => $this->params],
|
||||||
];
|
];
|
||||||
if ($this->id) {
|
if ($this->model) {
|
||||||
Mailgateway::find($this->id)->update($payload);
|
$this->model->update($payload);
|
||||||
} else {
|
} else {
|
||||||
Mailgateway::create($payload);
|
Mailgateway::create($payload);
|
||||||
}
|
}
|
||||||
|
@ -107,7 +107,7 @@ class Form extends Component
|
||||||
:label="$field['label']"
|
:label="$field['label']"
|
||||||
:type="$field['type']"
|
:type="$field['type']"
|
||||||
:name="$field['name']"
|
:name="$field['name']"
|
||||||
:required="str_contains('required', $field['storeValidator'])"
|
:required="str_contains('required', $field['validator'])"
|
||||||
></x-form::text>
|
></x-form::text>
|
||||||
@endforeach
|
@endforeach
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
<td>
|
<td>
|
||||||
<x-ui::action wire:click="$dispatch('openModal', {
|
<x-ui::action wire:click="$dispatch('openModal', {
|
||||||
component: 'modules.mailgateway.components.form',
|
component: 'modules.mailgateway.components.form',
|
||||||
props: {model: '{{$gateway->id}}'},
|
props: {id: '{{$gateway->id}}'},
|
||||||
title: 'Verbindung {{$gateway->name}} bearbeiten'}
|
title: 'Verbindung {{$gateway->name}} bearbeiten'}
|
||||||
)" icon="pencil" variant="warning">Bearbeiten</x-ui::action>
|
)" icon="pencil" variant="warning">Bearbeiten</x-ui::action>
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -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']);
|
$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']);
|
$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])
|
||||||
->assertSet('id', $mailgateway->id)
|
->assertSet('model', fn ($m) => $m->is($mailgateway))
|
||||||
->assertSet('name', '::name::')
|
->assertSet('name', '::name::')
|
||||||
->assertSet('domain', 'example.com')
|
->assertSet('domain', 'example.com')
|
||||||
->assertSet('cls', MailmanType::class)
|
->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']);
|
$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('name', '::name::')
|
||||||
->assertSet('domain', 'example.com')
|
->assertSet('domain', 'example.com')
|
||||||
->assertSet('cls', LocalType::class)
|
->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']);
|
$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', '')
|
->set('cls', '')
|
||||||
->assertHasErrors(['cls' => 'required']);
|
->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']);
|
$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']);
|
$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::')
|
->set('name', '::newname::')
|
||||||
->call('onSave')
|
->call('onSave')
|
||||||
->assertHasNoErrors()
|
->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']);
|
$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();
|
$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')
|
->set('params.user', 'newuser')
|
||||||
->call('onSave')
|
->call('onSave')
|
||||||
->assertHasNoErrors();
|
->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']);
|
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();
|
$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')
|
->set('params.user', 'newuser')
|
||||||
->call('onSave')
|
->call('onSave')
|
||||||
->assertHasErrors('connection');
|
->assertHasErrors('connection');
|
||||||
|
|
Loading…
Reference in New Issue