Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
adamyulian opened this issue Jan 17, 2024 · 0 comments
Open

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

adamyulian opened this issue Jan 17, 2024 · 0 comments

Comments

@adamyulian
Copy link

adamyulian commented Jan 17, 2024

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']);}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant