F
Filament12mo ago
ocram82

Custom form field inside resource

Hi, i want to insert custom field inside a resource and i'm following this: https://filamentphp.com/docs/3.x/forms/fields/custom#custom-field-classes. All was ok untill the moment to define my inputs a wire:model. I've an array of input and i don't know how to define wire:model. I also don't know with simple example:
<input wire:model="name" />
<input wire:model="name" />
How i can define or retrieve property name inside a resource?
10 Replies
Lara Zeus
Lara Zeus12mo ago
use
<input wire:model="{{ $getStatePath() }}" />
<input wire:model="{{ $getStatePath() }}" />
the getStatePath is what you used in the make() of your custom field
ocram82
ocram82OP12mo ago
ok thanks, but how can i retrieve and save it? consider that i have an array and i want to build a custom function to save only that field
public static function form(Form $form): Form
{
return $form
->schema([
...
Section::make('Week Hours')
->schema([
TeacherHourInput::make('hours')->label('Hours'),
])->compact()
...
public static function form(Form $form): Form
{
return $form
->schema([
...
Section::make('Week Hours')
->schema([
TeacherHourInput::make('hours')->label('Hours'),
])->compact()
...
Lara Zeus
Lara Zeus12mo ago
I think the saving prossess will be done in the resource or using some hooks
ocram82
ocram82OP12mo ago
this seems to work. The only problem now is that TeacherHourInput::make('hours')->label('Hours'), hours should be an array because i've an array of select:
@foreach(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] as $indexDay => $day)
@foreach ([1,2,3,4,5,6] as $hour)
<td class="text-gray-900 text-center sm:pr-0 border border-cyan-600">
<div class="relative">
<select wire:model="data.teacherHour.{{ $indexDay }}.{{ $hour }}" class="pl-1 pr-0 py-2 w-full bg-transparent text-xs appearance-none border-none">
<option value=""></option>
@foreach ([1,2,3] as $class)
@foreach (['A','B','C','D','E','F'] as $section)
<option value="{{ $class }},{{ $section }}">{{ $class }}{{ $section }} </option>
@endforeach
@endforeach
</select>
</div>
</td>
@endforeach
@endforeach
@foreach(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] as $indexDay => $day)
@foreach ([1,2,3,4,5,6] as $hour)
<td class="text-gray-900 text-center sm:pr-0 border border-cyan-600">
<div class="relative">
<select wire:model="data.teacherHour.{{ $indexDay }}.{{ $hour }}" class="pl-1 pr-0 py-2 w-full bg-transparent text-xs appearance-none border-none">
<option value=""></option>
@foreach ([1,2,3] as $class)
@foreach (['A','B','C','D','E','F'] as $section)
<option value="{{ $class }},{{ $section }}">{{ $class }}{{ $section }} </option>
@endforeach
@endforeach
</select>
</div>
</td>
@endforeach
@endforeach
this: wire:model="data.teacherHour.{{ $indexDay }}.{{ $hour }}" seems don't work
Lara Zeus
Lara Zeus12mo ago
this is similler to what i did here https://github.com/lara-zeus/matrix-choice/blob/3.x/resources/views/components/matrix-choice.blade.php see how it working hope it help also did you cast your field to array in the model?
GitHub
matrix-choice/resources/views/components/matrix-choice.blade.php at...
multiple choice grid form component for filamentPHP - lara-zeus/matrix-choice
ocram82
ocram82OP12mo ago
Cast is good option. How can i do that? i think this piece of code is the way! how do you treated {{ $applyStateBindingModifiers('wire:model') }}="{{ $supStatPath }}" backend side?
Lara Zeus
Lara Zeus12mo ago
when it cast to array you dont have to do anything 🙂 https://laravel.com/docs/master/eloquent-mutators#attribute-casting
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
ocram82
ocram82OP12mo ago
ah ok. thanks but this is not my case. I have created a custom Field in Filament with the view part as i posted above. My problem now is that i have a custom Field that is called teacherHour but the view contains an array of select that should be binded with that field in the resource:
public static function form(Form $form): Form
{
return $form
->schema([
...
->schema([
ViewField::make('teacherHour')
->view('forms.components.teacher-hour-input')
->label('Teacher Hour')
])
...
public static function form(Form $form): Form
{
return $form
->schema([
...
->schema([
ViewField::make('teacherHour')
->view('forms.components.teacher-hour-input')
->label('Teacher Hour')
])
...
and i don't know how to make teacherHour an array
Lara Zeus
Lara Zeus12mo ago
is teacherHour a column in your database? or a relationship? how you want to store them in the database?
ocram82
ocram82OP12mo ago
no there isn't or better: i've a Teacher model and i've a relationship with a TeacherTimetable model
class TeacherTimetable extends Model
{
...
protected $fillable = [
'organization_id',
'teacher_id',
'week_day',
'hour',
'class_number',
'class_char',
];
...
class TeacherTimetable extends Model
{
...
protected $fillable = [
'organization_id',
'teacher_id',
'week_day',
'hour',
'class_number',
'class_char',
];
...
But i want to handle in my custom way the update of that in the EditTeacher action with:
protected function handleRecordUpdate(Model $record, array $data): Model
{

$record->update($data);

return $record;
}
protected function handleRecordUpdate(Model $record, array $data): Model
{

$record->update($data);

return $record;
}
Now i'm getting myself in a simple scenario. the problem now is that i'm using this in the resource form
->schema([
TeacherHourInput::make('teacherHour')
->default('testtttest')
->view('forms.components.teacher-hour-input')
->label('Hour')
])
->schema([
TeacherHourInput::make('teacherHour')
->default('testtttest')
->view('forms.components.teacher-hour-input')
->label('Hour')
])
But i can't initialize or interact with the field in forms.components.teacher-hour-input
<x-dynamic-component :component="$getFieldWrapperView()" :field="$field">

<input type="text" wire:model="{{ $getStatePath() }}">

</x-dynamic-component>
<x-dynamic-component :component="$getFieldWrapperView()" :field="$field">

<input type="text" wire:model="{{ $getStatePath() }}">

</x-dynamic-component>
no way to set that input field to "testtttest" default value
Want results from more Discord servers?
Add your server