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\Wow;
use App\Http\Livewire\Traits\Alert;
use Livewire\Component;
class CardForm extends Component
class CardForm extends ModelForm
{
use Alert;
/**
* The card record
*/
public $card;
/**
* Array of available characters
*/
@ -31,13 +20,13 @@ class CardForm extends Component
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)
{
$this->card = $card;
$this->record = $card;
$this->characters = Character::all()->pluck('name', 'id');
$this->raids = Raid::all()->pluck('name', 'id');
$this->classes = Wow::$classes;
@ -50,17 +39,17 @@ class CardForm extends Component
protected function rules()
{
return [
'card.body' => 'required|string|min:3|max:200',
'card.character_id' => 'exists:' . Character::class . ',id|nullable',
'card.raid_id' => 'exists:' . Raid::class . ',id|nullable',
'card.class' => 'in:' . collect($this->classes)->keys() . '|nullable',
'record.body' => 'required|string|min:3|max:200',
'record.character_id' => 'exists:' . Character::class . ',id|nullable',
'record.raid_id' => 'exists:' . Raid::class . ',id|nullable',
'record.class' => 'in:' . collect($this->classes)->keys() . '|nullable',
];
}
public function updated($property, $value)
{
// 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)) {
$this->{$property} = null;
}
@ -68,37 +57,4 @@ class CardForm extends Component
$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;
use App\Models\Card;
use App\Models\Character;
use App\Models\Raid;
use App\Models\Wow;
use App\Http\Livewire\Traits\Alert;
use Livewire\Component;
class CharacterForm extends Component
class CharacterForm extends ModelForm
{
use Alert;
/**
* The character record
* Redirect after this route after record was created.
*/
public $character;
/**
* True if the record already exists in the database.
*/
public bool $exist;
public string $redirect_route = 'admin.character.index';
public function mount(Character $character)
{
$this->character = $character;
$this->record = $character;
$this->exist = $character->exists;
}
@ -37,45 +23,7 @@ class CharacterForm extends Component
protected function rules()
{
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\Http\Livewire\Traits\Alert;
use Livewire\Component;
class RaidForm extends Component
class RaidForm extends ModelForm
{
use Alert;
/**
* The raid record
* Redirect after this route after record was created.
*/
public $raid;
/**
* True if the record already exists in the database.
*/
public bool $exist;
public string $redirect_route = 'admin.raid.index';
public function mount(Raid $raid)
{
$this->raid = $raid;
$this->record = $raid;
$this->exist = $raid->exists;
}
@ -34,45 +23,7 @@ class RaidForm extends Component
protected function rules()
{
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');
}
}