F
Filament3mo ago
lmtc

Adding a value to a hidden field - or a suggestion

I'm trying to add a uuid to a hidden field in a builder component by default but it doesn't seem to be populating the value on load - I want to pass this value back into a different model as part of a builder action which is working but I just can't get the uuid in place:
->schema([
Hidden::make('form_styling_id')->live()->default(function() {
return Str::uuid();
}),
->schema([
Hidden::make('form_styling_id')->live()->default(function() {
return Str::uuid();
}),
Solution:
I've managed to crack it ``` ->fillForm(function ( array $arguments, FormsModel $record,...
Jump to solution
3 Replies
Tieme
Tieme3mo ago
Cant you set the UUID in model observer or on create hook?
lmtc
lmtc3mo ago
I've managed to do it like this:
Hidden::make('form_styling_id')->default(function() {
return Str::uuid()->toString();
}),
Hidden::make('form_styling_id')->default(function() {
return Str::uuid()->toString();
}),
which works on create but when I try to edit the field isn't filled with the default value in here:
->fillForm(function (array $arguments, FormsModel $record,BuilderComponent $component): array {
$item_data = $component->getItemState($arguments['item']);
$item_id = $item_data['form_styling_id'];


// Search for the FormsModel record based on the provided item_id
$found_record = FormStyling::find($item_id);
if ($found_record){
return [
'field_id' => $found_record->field_id,
'label' => $found_record->label,
];
} else { return [];
}

})
->fillForm(function (array $arguments, FormsModel $record,BuilderComponent $component): array {
$item_data = $component->getItemState($arguments['item']);
$item_id = $item_data['form_styling_id'];


// Search for the FormsModel record based on the provided item_id
$found_record = FormStyling::find($item_id);
if ($found_record){
return [
'field_id' => $found_record->field_id,
'label' => $found_record->label,
];
} else { return [];
}

})
It seem like the hidden field isn't pulling in the db value since that's saved correctly, but as soon as I click the builder action it clears the value
Solution
lmtc
lmtc3mo ago
I've managed to crack it
->fillForm(function (
array $arguments,
FormsModel $record,
BuilderComponent $component
): array {
$item_data = $component->getItemState($arguments['item']);
$form_styling_id = $item_data['form_styling_id'];
$found_record = FormStyling::where('field_id',
$form_styling_id)
->get();

if ($found_record->isNotEmpty()) {
$found_record = $found_record->first();
return [
'label' => $found_record->label,
];
} else {
return [];
}
})
->fillForm(function (
array $arguments,
FormsModel $record,
BuilderComponent $component
): array {
$item_data = $component->getItemState($arguments['item']);
$form_styling_id = $item_data['form_styling_id'];
$found_record = FormStyling::where('field_id',
$form_styling_id)
->get();

if ($found_record->isNotEmpty()) {
$found_record = $found_record->first();
return [
'label' => $found_record->label,
];
} else {
return [];
}
})
Want results from more Discord servers?
Add your server
More Posts