Accessing a form component from another
Hello,
I would like to know if it is possible to access another component from a component. For example, I'd like to modify the
placeholder
of a TextInput
according to a record field selected in a Select
.
I tried something like this, I can access the other component, but getSelectedRecord
is still null, as if the state didn't change
I've got this working, but I'd like if possible to avoid redoing an SQL request for nothing.
TY !15 Replies
you must use ->live() on field so after changing that field the screen reload with new state
@skashizadeh both field are
reactive
(sorry I didn't put in my example), it changes nothing 😦may you put your actual code here?
I tried many thing, here with
afterStateUpdated
in the Select
what version of filament do you use?
latest
:/ change reactive to live. i did not see reactive anywhere
It's the same thing 😛 (take a look)
Have you ever managed to mutate one component from another? (wihtout using Set)
never needed, always used get / set to do the work
Me too. but in this case I need to mutate a property (placeholder) 😢
you want to get selected item and put its value as placeholder inside title?
nop, I want to get the selectedItem (model) title and put its value as placeholder inside the title component
This not working too 😦
$component->getLivewire()->getForm('form')->getComponent('type_event_id')
return the select component, but getSelectedRecord
always return nullTRY THIS
[
Forms\Components\Select::make('type_event_id')
->label(('labels.type'))
->required()
->reactive()
->prefixIcon(fn (?string $state) => !is_null($state)
? 'heroicon-m-stop'
: null
)
->afterStateUpdated(function ($state, $set, $get) {
// Trigger a re-render
$set('title', $get('title') . '');
$set('titlePlaceholder', "$state");
})
->prefixIconColor(function (Forms\Components\Select $component) {
$color = $component->getSelectedRecord()?->background_color;
return !is_null($color) ? Color::hex($color) : null;
})
->relationship(
'typeEvent',
'name'
)
->createOptionForm(TypeEventResource::formSchema())
->createOptionAction(
fn (Forms\Components\Actions\Action $action) => $action
->slideOver()
->modalWidth('xl'),
),
Forms\Components\TextInput::make('title')
->label(('labels.title'))
->placeholder(fn ($get) => $get('titlePlaceholder') ?? 'Default placeholder')
->reactive(),
]
@skashizadeh it's a bit ugly but it's working
But instead adding shit in state, I would prefer to directly interact with the component...
it looks nice