emilianotisato / nova-google-autocomplete-field

Address Autocomplete with Google Place API (full metadata available)
20 stars 17 forks source link

Get value from fallback field #20

Open meyer59 opened 3 years ago

meyer59 commented 3 years ago

Hello Is it possible to get the value from a fallback field ? For example: let's say you have a state field in your db, In the Google place response the state field is not always filled so you would rather fetch the value from the administrative_area_level_1. This could also be the case for the city: for some city the locality is filled and for some others the vicinity is filled. It could be great if we can specify fallback field for the same db value. Cordially

emilianotisato commented 3 years ago

Sorry @meyer59 I did't test it with a fallback field. But I am guessing it should work ok

If you can make it work please let me know. Or feel free to send a PR with a possible solution, I will gladly take a look at your PR

AndPicc commented 2 years ago

hello! I have a similar problem and I think it would be enough if you could print out an empty string in case the value is not present when using the "combined" syntax for fromValue().

so like ->fromValue('{{locality}}{{postal_town}}')

at the moment the above will print either one of these cases:

undefinedCity Name
City Nameundefined

so if you add a check for "defined" before printing, this workaround would print out only the locality when it's present, OR the postal_town when there is that one instead.

p.s. thanks for this package! it really helped out a lot now that Nova Places is being deprecated.

asad-mytake commented 10 months ago

hello! I have a similar problem and I think it would be enough if you could print out an empty string in case the value is not present when using the "combined" syntax for fromValue().

so like ->fromValue('{{locality}}{{postal_town}}')

at the moment the above will print either one of these cases:

undefinedCity Name
City Nameundefined

so if you add a check for "defined" before printing, this workaround would print out only the locality when it's present, OR the postal_town when there is that one instead.

p.s. thanks for this package! it really helped out a lot now that Nova Places is being deprecated.

where exactly we have to put this defined check, will you please show me the code of that. i exactly want the same thing you mentioned above, if route is not defined then it shows undefined into concatination case (if i use {{parm1}} {{parm2}} it shows undefined if each of param is not defined)

teamappelit commented 10 months ago

@asad-mytake facing the same issue when we add like {{route}} {{street_number}} in the AddressMetaField. So, if you resolved this issue or were able to find out any workaround then please share it here.

GoogleAutocomplete::make(__('Address'), 'address')
        ->nullable()
        ->rules('required', 'string', 'max:255')
        ->withValues([
            'route',
            'street_number',
            'postal_code',
            'locality',
            'administrative_area_level_1.long_name',
            'country.short_name',
            'latitude',
            'longitude',
        ])->placeType('address'),

  AddressMetadata::make(__('Address Line 1'), 'address_line_1')
      ->sortable()
      ->nullable()
      ->rules('nullable', 'required_with:address', 'string', 'max:255')
      ->fromValue('{{route}} {{street_number}}'),

when route and street_number do not exist then it become show in the AddressMetaField as undefined undefined.

AndPicc commented 10 months ago

@asad-mytake I am sorry but mine was a suggestion to the package developer to introduce a conditional within its code, and skip the value when it's undefined rather than printing out "undefined".

Unfortunately we couldn't find a solution achievable with code from our side. In the end we just decided to pull from the fields that are more commonly used in UK. Though the address is not a vital component on that website, and it's not tied with any functionality (i.e. we don't have to send parcels nor create official documents), hence it doesn't matter if it's not super precise.