Simplify form
This commit is contained in:
		
							parent
							
								
									1c5a9681b2
								
							
						
					
					
						commit
						cbe53369e7
					
				|  | @ -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' => '', | ||||
|             ], | ||||
|         ]; | ||||
|  |  | |||
|  | @ -60,10 +60,10 @@ abstract class Type | |||
|     /** | ||||
|      * @return array<string, mixed> | ||||
|      */ | ||||
|     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(); | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -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'])" | ||||
|                         ></x-form::text> | ||||
|                     @endforeach | ||||
|                 </form> | ||||
|  |  | |||
|  | @ -26,7 +26,7 @@ | |||
|                 <td> | ||||
|                     <x-ui::action wire:click="$dispatch('openModal', { | ||||
|                         component: 'modules.mailgateway.components.form', | ||||
|                         props: {model: '{{$gateway->id}}'}, | ||||
|                         props: {id: '{{$gateway->id}}'}, | ||||
|                         title: 'Verbindung {{$gateway->name}} bearbeiten'} | ||||
|                     )" icon="pencil" variant="warning">Bearbeiten</x-ui::action>
 | ||||
|                 </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']); | ||||
|     $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'); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue