70 lines
1.5 KiB
PHP
70 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire\Form;
|
|
|
|
use App\Models\Card;
|
|
use App\Models\Character;
|
|
use App\Models\Raid;
|
|
use App\Models\Wow;
|
|
|
|
class CardForm extends ModelForm
|
|
{
|
|
/**
|
|
* Array of available characters
|
|
*/
|
|
public $characters;
|
|
|
|
/**
|
|
* Array of available raids
|
|
*/
|
|
public $raids;
|
|
|
|
/**
|
|
* Redirect after this route after record was created.
|
|
*/
|
|
public string $redirect_route = 'admin.card.index';
|
|
|
|
public function mount(Card $card)
|
|
{
|
|
$this->record = $card;
|
|
$this->characters = Character::all()->pluck('name', 'id');
|
|
$this->raids = Raid::all()->pluck('name', 'id');
|
|
$this->classes = Wow::$classes;
|
|
$this->exist = $card->exists;
|
|
}
|
|
|
|
/**
|
|
* Validation rules
|
|
*/
|
|
protected function rules()
|
|
{
|
|
return [
|
|
'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, ['record.character_id', 'record.raid_id', 'record.class'])) {
|
|
if (empty($value)) {
|
|
$this->{$property} = null;
|
|
}
|
|
}
|
|
|
|
$this->validateOnly($property);
|
|
}
|
|
|
|
public function getSubjectProperty()
|
|
{
|
|
if ($this->record->character_id) {
|
|
return $this->characters[$this->record->character_id];
|
|
} else if ($this->record->class) {
|
|
return $this->classes[$this->record->class];
|
|
}
|
|
return "Somebody";
|
|
}
|
|
}
|