Livewire component with modal $this->form->fill() not working?

I'm trying to make it so when a user clicks on an item in my list, a slideOver modal shows and is passed the specific item to get the data for and populate the field. However, if I do @json($this->form) it shows an empty model, and if i do @json($this->form->getState() I get the error No property found for validation: [data.title]. Here's the livewire component:
class EditMonthEndTask extends Component implements HasForms, HasInfolists
{
use InteractsWithInfolists, InteractsWithForms;

protected MonthEndTask $task;
protected Team $team;

protected $listeners = ['editTask'];

public function mount(): void
{
$this->task = new MonthEndTask();
$this->team = new Team();
$this->form->fill([$this->task->toArray()]);
}

public function render()
{
return view('livewire.components.month-end.edit-month-end-task', ['task' => $this->task]);
}

public function editTask($id)
{
$this->task = MonthEndTask::find($id);
$this->team = $this->task->location->team->load('members');
$this->form->fill([$this->task->toArray()]);
$this->dispatch('open-modal', id: 'edit-month-end-task', task: $this->task);
}

public function form(Form $form): Form
{
return $form->schema([
TextInput::make('title')->required(),
Select::make('assigned_to')
->options($this->team->members->pluck('fullName', 'id')->toArray())
])->statePath('data');
}
}
class EditMonthEndTask extends Component implements HasForms, HasInfolists
{
use InteractsWithInfolists, InteractsWithForms;

protected MonthEndTask $task;
protected Team $team;

protected $listeners = ['editTask'];

public function mount(): void
{
$this->task = new MonthEndTask();
$this->team = new Team();
$this->form->fill([$this->task->toArray()]);
}

public function render()
{
return view('livewire.components.month-end.edit-month-end-task', ['task' => $this->task]);
}

public function editTask($id)
{
$this->task = MonthEndTask::find($id);
$this->team = $this->task->location->team->load('members');
$this->form->fill([$this->task->toArray()]);
$this->dispatch('open-modal', id: 'edit-month-end-task', task: $this->task);
}

public function form(Form $form): Form
{
return $form->schema([
TextInput::make('title')->required(),
Select::make('assigned_to')
->options($this->team->members->pluck('fullName', 'id')->toArray())
])->statePath('data');
}
}
Solution:
since you're using ->statePath('data') you need to add public $data = [];...
Jump to solution
2 Replies
Solution
Lara Zeus
Lara Zeus14mo ago
since you're using ->statePath('data') you need to add public $data = [];
Jon Mason
Jon MasonOP14mo ago
well that was stupid. I probably should've realized that. Thanks for clueing me in!
Want results from more Discord servers?
Add your server