Archived
1
0
Fork 0

Adding CharacterForm livewire component

This commit is contained in:
Henrik Hautakoski 2022-01-09 10:42:06 +01:00
parent 8a95143b8f
commit 4a7c2dd66c
2 changed files with 98 additions and 0 deletions

View file

@ -0,0 +1,81 @@
<?php
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
{
use Alert;
/**
* The character record
*/
public $character;
/**
* True if the record already exists in the database.
*/
public bool $exist;
public function mount(Character $character)
{
$this->character = $character;
$this->exist = $character->exists;
}
/**
* Validation rules
*/
protected function rules()
{
return [
'character.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

@ -0,0 +1,17 @@
<div class="space-y-4">
<x-header>{{ __(($this->isNew() ? 'Create' : 'Edit') . ' character') }}</x-header>
<form class="space-y-4" wire:submit.prevent="submit">
<div>
<x-ignite-label for="character.name">{{ __('Name') }}</x-ignite-label>
<x-ignite-input name="character.name" wire:model="character.name" />
</div>
<x-button type="info">{{ __('Save') }}</x-button>
<x-button href="{{ route('admin.character.index') }}" type="warning">{{ __('Back') }}</x-button>
</form>
</div>