whitecube / nova-flexible-content

Flexible Content & Repeater Fields for Laravel Nova
MIT License
780 stars 229 forks source link

Populate Select field with options from the same Layout #453

Open Epizefiri opened 1 year ago

Epizefiri commented 1 year ago

Hello there, I've several locations by a Flexible field, when I save a location I get through an observer some "memberships". I would like to use this memberships as options for a Select field. How do I pass the options to the field?

                Flexible::make('Locali', 'locations')
                ->addLayout('Impostazioni Locale', 'location', [
                    Text::make('Nome Locale', 'name'),
                    Text::make('Ipratico Key', 'ipratico_key'),
                    Image::make('Logo','location_logo')->deletable(false)->disableDownload(),
                    Image::make('Banner','location_banner')->deletable(false)->disableDownload(),
                    Boolean::make('Usa immagini default', 'default_images'),
                    Textarea::make('Testo Success Page', 'success_page'),
                    Select::make('Membership', 'membership')->options(

// here my problem //

                    )->displayUsingLabels(),
                ])->button('Aggiungi locale'),  
SenidSM commented 4 months ago

I'm having a similar issue, I have two Selects (country and city) they work perfectly fine... If outside the Flexible field. If inside the Flexible only the country works, as soon as I select a country the City Select literally disappears.

Has anyone being able to fix this?

This works:

 Select::make('Country', 'country')->searchable()
                        ->options(WorldCountry::orderBy('name')->get()->pluck('name', 'name'))
                        ->rules('required'),

                    Select::make('City', 'city')
                        ->dependsOn(
                            ['country'],
                                function (Select $field, NovaRequest $request, FormData $formData) {

                                    $country = WorldCountry::getByName($formData->country);
                                    if ($country){
                                        $field->options($country->cities()->orderBy('name')->pluck('name'));
                                    }
                                }
                    )->searchable()

This does not:

Flexible::make('Locations', 'location')
                ->button('Add location')
                ->addLayout('Location', 'location', [
                    Select::make('Country', 'country')->searchable()
                        ->options(WorldCountry::orderBy('name')->get()->pluck('name', 'name'))
                        ->rules('required'),

                    Select::make('City', 'city')
                        ->dependsOn(
                            ['country'],
                                function (Select $field, NovaRequest $request, FormData $formData) {

                                    $country = WorldCountry::getByName($formData->country);
                                    if ($country){
                                        $field->options($country->cities()->orderBy('name')->pluck('name'));
                                    }
                                }
                    )->searchable()
                ])->rules('required')

EDIT - SOLUTION

This solves the issue, is a fork so should be removed as soon as the support is added: https://github.com/formfeed-uk/nova-flexible-content