Archived
1
0
Fork 0

app/Http/Livewire/Form/*: extend ModelForm to share ALOT of code and make concrete form classes smaller

This commit is contained in:
Henrik Hautakoski 2022-01-09 11:11:58 +01:00
parent 5f960cf3d9
commit 40b73e1c5c
6 changed files with 31 additions and 176 deletions

View file

@ -7,19 +7,8 @@ use App\Models\Character;
use App\Models\Raid; use App\Models\Raid;
use App\Models\Wow; use App\Models\Wow;
use App\Http\Livewire\Traits\Alert; class CardForm extends ModelForm
use Livewire\Component;
class CardForm extends Component
{ {
use Alert;
/**
* The card record
*/
public $card;
/** /**
* Array of available characters * Array of available characters
*/ */
@ -31,13 +20,13 @@ class CardForm extends Component
public $raids; public $raids;
/** /**
* True if the record already exists in the database. * Redirect after this route after record was created.
*/ */
public bool $exist; public string $redirect_route = 'admin.card.index';
public function mount(Card $card) public function mount(Card $card)
{ {
$this->card = $card; $this->record = $card;
$this->characters = Character::all()->pluck('name', 'id'); $this->characters = Character::all()->pluck('name', 'id');
$this->raids = Raid::all()->pluck('name', 'id'); $this->raids = Raid::all()->pluck('name', 'id');
$this->classes = Wow::$classes; $this->classes = Wow::$classes;
@ -50,17 +39,17 @@ class CardForm extends Component
protected function rules() protected function rules()
{ {
return [ return [
'card.body' => 'required|string|min:3|max:200', 'record.body' => 'required|string|min:3|max:200',
'card.character_id' => 'exists:' . Character::class . ',id|nullable', 'record.character_id' => 'exists:' . Character::class . ',id|nullable',
'card.raid_id' => 'exists:' . Raid::class . ',id|nullable', 'record.raid_id' => 'exists:' . Raid::class . ',id|nullable',
'card.class' => 'in:' . collect($this->classes)->keys() . '|nullable', 'record.class' => 'in:' . collect($this->classes)->keys() . '|nullable',
]; ];
} }
public function updated($property, $value) public function updated($property, $value)
{ {
// Hack to force empty value to null. // Hack to force empty value to null.
if (in_array($property, ['card.character_id', 'card.raid_id', 'card.class'])) { if (in_array($property, ['record.character_id', 'record.raid_id', 'record.class'])) {
if (empty($value)) { if (empty($value)) {
$this->{$property} = null; $this->{$property} = null;
} }
@ -68,37 +57,4 @@ class CardForm extends Component
$this->validateOnly($property); $this->validateOnly($property);
} }
/**
* Returns true if this card has not been stored in the database.
*/
public function isNew() : bool
{
return !$this->exist;
}
/**
* Submit the form, create/update card.
*/
public function submit()
{
$this->validate();
$this->card->save();
if ($this->isNew()) {
session()->flash('info', 'Card was successfully created.');
return redirect()->route('admin.card.index');
}
$this->info('Card was successfully updated.');
}
/**
* Render the setup page
*/
public function render()
{
return view('form.card')
->layout('layouts.admin');
}
} }

View file

@ -2,32 +2,18 @@
namespace App\Http\Livewire\Form; namespace App\Http\Livewire\Form;
use App\Models\Card;
use App\Models\Character; use App\Models\Character;
use App\Models\Raid;
use App\Models\Wow;
use App\Http\Livewire\Traits\Alert; class CharacterForm extends ModelForm
use Livewire\Component;
class CharacterForm extends Component
{ {
use Alert;
/** /**
* The character record * Redirect after this route after record was created.
*/ */
public $character; public string $redirect_route = 'admin.character.index';
/**
* True if the record already exists in the database.
*/
public bool $exist;
public function mount(Character $character) public function mount(Character $character)
{ {
$this->character = $character; $this->record = $character;
$this->exist = $character->exists; $this->exist = $character->exists;
} }
@ -37,45 +23,7 @@ class CharacterForm extends Component
protected function rules() protected function rules()
{ {
return [ return [
'character.name' => 'required|string|min:3|max:14', 'record.name' => 'required|string|min:3|max:14',
]; ];
} }
public function updated($property, $value)
{
$this->validateOnly($property);
}
/**
* Returns true if this card has not been stored in the database.
*/
public function isNew() : bool
{
return !$this->exist;
}
/**
* Submit the form, create/update card.
*/
public function submit()
{
$this->validate();
$this->character->save();
if ($this->isNew()) {
session()->flash('info', 'Character was successfully created.');
return redirect()->route('admin.character.index');
}
$this->info('Character was successfully updated.');
}
/**
* Render the setup page
*/
public function render()
{
return view('form.character')
->layout('layouts.admin');
}
} }

View file

@ -4,27 +4,16 @@ namespace App\Http\Livewire\Form;
use App\Models\Raid; use App\Models\Raid;
use App\Http\Livewire\Traits\Alert; class RaidForm extends ModelForm
use Livewire\Component;
class RaidForm extends Component
{ {
use Alert;
/** /**
* The raid record * Redirect after this route after record was created.
*/ */
public $raid; public string $redirect_route = 'admin.raid.index';
/**
* True if the record already exists in the database.
*/
public bool $exist;
public function mount(Raid $raid) public function mount(Raid $raid)
{ {
$this->raid = $raid; $this->record = $raid;
$this->exist = $raid->exists; $this->exist = $raid->exists;
} }
@ -34,45 +23,7 @@ class RaidForm extends Component
protected function rules() protected function rules()
{ {
return [ return [
'raid.name' => 'required|string|min:2|max:20', 'record.name' => 'required|string|min:2|max:20',
]; ];
} }
public function updated($property, $value)
{
$this->validateOnly($property);
}
/**
* Returns true if this card has not been stored in the database.
*/
public function isNew() : bool
{
return !$this->exist;
}
/**
* Submit the form, create/update card.
*/
public function submit()
{
$this->validate();
$this->raid->save();
if ($this->isNew()) {
session()->flash('info', 'Raid was successfully created.');
return redirect()->route('admin.character.index');
}
$this->info('Raid was successfully updated.');
}
/**
* Render the setup page
*/
public function render()
{
return view('form.raid')
->layout('layouts.admin');
}
} }

View file

@ -6,22 +6,22 @@
<form class="space-y-4" wire:submit.prevent="submit"> <form class="space-y-4" wire:submit.prevent="submit">
<div> <div>
<x-ignite-label for="card.body">{{ __('Body') }}</x-ignite-label> <x-ignite-label for="record.body">{{ __('Body') }}</x-ignite-label>
<x-ignite-input name="card.body" wire:model="card.body" /> <x-ignite-input name="record.body" wire:model="record.body" />
</div> </div>
<div class="grid grid-cols-3 space-x-4"> <div class="grid grid-cols-3 space-x-4">
<div> <div>
<x-ignite-label for="card.raid_id">{{ __('Raid') }}</x-ignite-label> <x-ignite-label for="record.raid_id">{{ __('Raid') }}</x-ignite-label>
<x-ignite-select name="card.raid_id" wire:model="card.raid_id" :options="$raids" placeholder="{{ __('Select a raid') }}" /> <x-ignite-select name="record.raid_id" wire:model="record.raid_id" :options="$raids" placeholder="{{ __('Select a raid') }}" />
</div> </div>
<div> <div>
<x-ignite-label for="card.character_id">{{ __('Character') }}</x-ignite-label> <x-ignite-label for="record.character_id">{{ __('Character') }}</x-ignite-label>
<x-ignite-select name="card.character_id" wire:model="card.character_id" :options="$characters" placeholder="{{ __('Select a character') }}" /> <x-ignite-select name="record.character_id" wire:model="record.character_id" :options="$characters" placeholder="{{ __('Select a character') }}" />
</div> </div>
<div> <div>
<x-ignite-label for="card.class">{{ __('Class') }}</x-ignite-label> <x-ignite-label for="record.class">{{ __('Class') }}</x-ignite-label>
<x-ignite-select name="card.class" wire:model="card.class" :options="$classes" placeholder="{{ __('Select a class') }}" /> <x-ignite-select name="record.class" wire:model="record.class" :options="$classes" placeholder="{{ __('Select a class') }}" />
</div> </div>
</div> </div>

View file

@ -6,8 +6,8 @@
<form class="space-y-4" wire:submit.prevent="submit"> <form class="space-y-4" wire:submit.prevent="submit">
<div> <div>
<x-ignite-label for="character.name">{{ __('Name') }}</x-ignite-label> <x-ignite-label for="record.name">{{ __('Name') }}</x-ignite-label>
<x-ignite-input name="character.name" wire:model="character.name" /> <x-ignite-input name="record.name" wire:model="record.name" />
</div> </div>
<x-button type="info">{{ __('Save') }}</x-button> <x-button type="info">{{ __('Save') }}</x-button>

View file

@ -6,8 +6,8 @@
<form class="space-y-4" wire:submit.prevent="submit"> <form class="space-y-4" wire:submit.prevent="submit">
<div> <div>
<x-ignite-label for="raid.name">{{ __('Name') }}</x-ignite-label> <x-ignite-label for="record.name">{{ __('Name') }}</x-ignite-label>
<x-ignite-input name="raid.name" wire:model="raid.name" /> <x-ignite-input name="record.name" wire:model="record.name" />
</div> </div>
<x-button type="info">{{ __('Save') }}</x-button> <x-button type="info">{{ __('Save') }}</x-button>