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');
    }


But a much easier way would be if I could simply $get the field value from another formgroup. Does anyone have idea about this?
Was this page helpful?