Help accessing parent form field value

Since I could not $get the field value when trying to access the field value I found a hack to set form variables like this:
public static function getCakeFlavorsFormField(): Select
{
return Select::make('flavors_list')
->relationship('flavors', 'name') // We need this here since it must load the set data from pivot table
->multiple()
->options(Flavor::getFlavorsPluck('active')->toArray())
->preload()
->reactive()
->afterStateUpdated(function (callable $set, $state) {
// $state is the selected flavors
if (!$state) {
$flavors = Flavor::getFlavorsPluck('active')->toArray();
} else {
$flavors = Flavor::getFlavorsPluck('active')->only($state)->toArray();
}

// Set 'flavors' for the 'cake' group
$set('cake.selectedFlavors', $flavors);
$set('cake.flavor_id', null); // Reset the default flavor whenever flavors_list changes
})->required();
}
public static function getDefaultCakeFlavorFormField(): Group
{
return Group::make()->schema([
Select::make('flavor_id')->label('Default Flavor')
->options(function (callable $get): array {

// I could not do $get('flavors_list') and get the values directly. This is always null

$flavors = $get('selectedFlavors');
if ($flavors) {
return $get('selectedFlavors');
}
return Flavor::getFlavorsPluck('active')->toArray();
})
])->relationship('cake');
}
public static function getCakeFlavorsFormField(): Select
{
return Select::make('flavors_list')
->relationship('flavors', 'name') // We need this here since it must load the set data from pivot table
->multiple()
->options(Flavor::getFlavorsPluck('active')->toArray())
->preload()
->reactive()
->afterStateUpdated(function (callable $set, $state) {
// $state is the selected flavors
if (!$state) {
$flavors = Flavor::getFlavorsPluck('active')->toArray();
} else {
$flavors = Flavor::getFlavorsPluck('active')->only($state)->toArray();
}

// Set 'flavors' for the 'cake' group
$set('cake.selectedFlavors', $flavors);
$set('cake.flavor_id', null); // Reset the default flavor whenever flavors_list changes
})->required();
}
public static function getDefaultCakeFlavorFormField(): Group
{
return Group::make()->schema([
Select::make('flavor_id')->label('Default Flavor')
->options(function (callable $get): array {

// I could not do $get('flavors_list') and get the values directly. This is always null

$flavors = $get('selectedFlavors');
if ($flavors) {
return $get('selectedFlavors');
}
return Flavor::getFlavorsPluck('active')->toArray();
})
])->relationship('cake');
}
But a much easier way would be if I could simply $get the field value from another formgroup. Does anyone have idea about this?
3 Replies
Patrick Boivin
I'm not 100% sure but I think the reason $get() doesn't work is because your second Group is scoped to the cake relationship Have you tried $get('../flavors_list') ?
moaaz_m_f
moaaz_m_f2y ago
#✅┊rules Kindly add your code this way
public function myCodeGoesHere(){};
public function myCodeGoesHere(){};
Kiran Timsina
Kiran TimsinaOP2y ago
Yes. Will try ../ way.
Want results from more Discord servers?
Add your server