cheesegrits / filament-google-maps

Google Maps package for Filament PHP
212 stars 60 forks source link

Problems with ->Rules() and dependant fields value. #73

Open adamyulian opened 6 months ago

adamyulian commented 6 months ago

I have a Rules (called AttendanceRadius) to allow only person in specific area which can store/create form. the rule have 3 attributes : latitude, longitude, radius. I can't get solution how to call the three attributes from dependant field, I try this following code but it doesn't work because this code return Closure not a value. the rule work when I input specific value (numeric for lat, long, and rad)). But I still can't how to make them have a dynamic value by dependant from first fields (target_id).

Forms\Components\Select::make('target_id')
                        ->columnSpan(4)
                        ->relationship(
                            name: 'Target', 
                            titleAttribute: 'nama',
                            modifyQueryUsing: function (Builder $query) {
                                $teamname = Auth::user()->team->name;
                                $query->where('surveyor', $teamname)
                                ->where('user_id', 0)
                                ;}
                            )
                        ->getOptionLabelFromRecordUsing(fn (Target $record) => "{$record->register} {$record->nama} {$record->alamat}")
                        ->searchable(['register', 'nama', 'alamat'])
                        ->live(onBlur:true)
                        ->lazy()
                        ->afterStateUpdated(function (string $state, callable $get, Forms\Set $set) {
                            $set('name', Target::find($state)->nama);
                            $set('luas', Target::find($state)->luas);
                            $set('tahun_perolehan', Target::find($state)->tahun_perolehan);
                            $set('penggunaan', Target::find($state)->penggunaan);
                            $set('alamat', Target::find($state)->alamat);
                            $set('target_id1', Target::find($state)->id);
                            $set('latitude',Target::find($state)->lat);
                            $set('longitude',Target::find($state)->lng);

                            $set('location_target', 
                            [
                                'lat' => floatval(Target::find($state)->lat),
                                'lng' => floatval(Target::find($state)->lng)
                            ]);
                                                   }),
Map::make('location')
                                    ->columnSpan(2)
                                    ->rules([new AttendanceRadius(function (Get $get): float {
                                        return Target::find($get('target_id'))->lat;},
                                        function (Get $get) { 
                                            return Target::find($get('target_id'))->lng;
                                        },
                                        100
                                    )])
                                    ->label('Your Location')
                                    ->geolocate() // adds a button to request device location and set map marker accordingly
                                    ->geolocateOnLoad(true, 'always')// Enable geolocation on load for every form
                                    ->draggable(false) // Disable dragging to move the marker
                                    ->clickable(false) // Disable clicking to move the marker
                                    ->defaultZoom(15) // Set the initial zoom level to 500
                                    ->autocomplete('note') // field on form to use as Places geocompletion field
                                    ->autocompleteReverse(true) // reverse geocode marker location to autocomplete field
                                    ->reactive()
                                    ->live()
                                    ->afterStateUpdated(function ($state, callable $get, callable $set) {
                                        $set('lat', $state['lat']);
                                        $set('lng', $state['lng']);}),