Custom Widget variable $state doesn't works

I'm working in a personal project, it's a Gym system to personal trainers. One of the requirements in this system, it's a widget in the home page this widget search the "Student" when the Student is selected i need to create a atendance. My first step to create this component is use the command to create the widget: php artisan make:filament-widget AttendanceSearchWidget Type of widget: Custom Resource: no Panel: admin After the widget was created i implement the interface "HasForms" in the Widget class using the trait use Forms\Concerns\InteractsWithForms; To display the form on the main screen I added the widget to the AdminPanel and modified the widget's blade.php. I created a Select form component in the Widget class to search all students and to create the attendance of students i create a suffixAction but i never can get the id of the students selectioned, everytime i try to get the Id of student the $state variable is null My Widget form class.
class AttendanceSearchWidget extends Widget implements HasForms { protected static ?string $model = Attendance::class; use Forms\Concerns\InteractsWithForms; protected static string $view = 'filament.widgets.attendance-search-widget'; protected function form(Form $form) : Form { return $form ->schema([ Forms\Components\Select::make('student_id') ->label('Marcar Presença') ->searchable() ->preload() ->options(fn() => Student::all()->pluck('name','id')) ->required() ->suffixAction( Forms\Components\Actions\Action::make('save') ->icon('heroicon-m-clipboard') ->action(fn ($state) => dd($state)) ) ]); } }
Thanks for your attention!
No description
No description
11 Replies
awcodes
awcodes3mo ago
Ok, you’ve created the form but what is the rest of it. What is supposed to be to happen with the form data. Seems like you just haven’t finished wiring everything together. Widgets are just livewire components.
Lucas de Mello
Lucas de MelloOP3mo ago
With the form's $state I don't have access to its data? I imagined that with suffixAction I could have access to the form data and use it to create my presence Example:
->suffixAction(
Forms\Components\Actions\Action::make('save')
->icon('heroicon-m-clipboard')
->action(fn ($state) =>
Attendance::create([
'student_id' => $state['student_id'],
'user_id' => auth()->user()->id,
'attendance_date' => now(),
]))

)
->suffixAction(
Forms\Components\Actions\Action::make('save')
->icon('heroicon-m-clipboard')
->action(fn ($state) =>
Attendance::create([
'student_id' => $state['student_id'],
'user_id' => auth()->user()->id,
'attendance_date' => now(),
]))

)
Lucas de Mello
Lucas de MelloOP3mo ago
Uhmm, I understand, we created the variable within Livewire to maintain the form data, but even when selecting the Element, the variables are not going into the data The browser console warning: Livewire: [wire:model="data.user_id"] property does not exist on component: [app.filament.widgets.attendance-search-widget] Livewire property ['data.student_id'] cannot be found on component:.... I tried to define student_id and user_id into the $data like this: public ?array $data = [ 'student_id'=> null, 'user_id'=> null ]; the errors and warnings disappear but the livewire doesn't read the values
Lucas de Mello
Lucas de MelloOP3mo ago
Not even the default value of the user_id in the form is it taking
No description
LeandroFerreira
LeandroFerreira3mo ago
share the whole code please
Lucas de Mello
Lucas de MelloOP3mo ago
GitHub
GitHub - lucassmelloo/gym360
Contribute to lucassmelloo/gym360 development by creating an account on GitHub.
LeandroFerreira
LeandroFerreira3mo ago
public function mount(): void
{
$this->form->fill();
}
public function mount(): void
{
$this->form->fill();
}
Lucas de Mello
Lucas de MelloOP3mo ago
This function works on Hidden component with default value but not in the select option
No description
LeandroFerreira
LeandroFerreira3mo ago
You need to add ->default() to the select
Lucas de Mello
Lucas de MelloOP3mo ago
With out ->default() works! Thank you soo mutch guys!

Did you find this page helpful?