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!
11 Replies
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.
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:
You’re not defining the statePath on the form. https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#adding-the-form
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
Not even the default value of the user_id in the form is it taking
share the whole code please
GitHub
GitHub - lucassmelloo/gym360
Contribute to lucassmelloo/gym360 development by creating an account on GitHub.
This function works on Hidden component with default value but not in the select option
You need to add
->default()
to the selectWith out ->default() works!
Thank you soo mutch guys!