app/Http/Livewire/Form/*: extend ModelForm to share ALOT of code and make concrete form classes smaller
This commit is contained in:
parent
5f960cf3d9
commit
40b73e1c5c
6 changed files with 31 additions and 176 deletions
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue