This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

graker
graker

Hello

I'm trying to create a plugin to output markers on google map. I have a Marker model with latitude and longitude fields (among others). I managed to create a simple form with number fields to input lat and lng. Now I want to improve UI and set up latitude and longitude by clicking on the map.

So I've created FormWidget and put it on Marker model fields.yaml with the name "coordinates". The widget renders two inputs - latitude and longitude - and I'd like to pass these input values to model's corresponding fields. But by default, the engine tries to save the "coordinates" field as it is the name of the form widget. And off course it fails because there's no such field in the model.

Now I tried some stuff to overwrite "coordinates" array in $_POST and in Request class with plain "latitude" and "longitude" fields but I'd still get an error that "coordinates" field it not in the database.

Looking at the code, it seems to me that Form::getSaveData() function just gets all form widget names and tries to get values from $_POST only for these names, ignoring any other values even if they match model fields. But maybe I'm missing something. So the question is - is it possible at all to create a FormWidget to support multiple model fields in one widget and to pass field values onto model without errors?

Last updated

Fencus
Fencus

Hello graker, I just made a plugin that manage this problem and I think it does what you need: https://octobercms.com/plugin/fencus-googlemapswidgets

Best regards. Fencus.

graker
graker

Fencus said:

Hello graker, I just made a plugin that manage this problem and I think it does what you need: https://octobercms.com/plugin/fencus-googlemapswidgets

Hi Fencus, thanks for your answer. I checked out your plugin documentation, it seems that it's not exactly what I'm looking for. It says that the latitude and longitude are returned as a string so it doesn't solve the problem of setting multiple model fields with one widget. What I want is to save lat and lng as two float values on my model. For now I haven't found a way to do so with a single widget so I just use two fields in my model's form to store lat&lng and set their values with a simple piece of JS code.

I don't have problems with Google map markers per se in my plugin, you can check it out in my github ( https://github.com/graker/mapmarkers ). It's nothing fancy and still needs a lot of work though. Even if I switched to your widget instead of using floats for lat&lng, the interest in solving the problem of multiple fields set by one widget remains.

Last updated

MarSch
MarSch

Hello Graker,

take a look here: https://octobercms.com/plugin/planetadeleste-widgets

One of the widgets contained in the plugin is called mappicker In documentation or by downloading the plugin you can see that a fieldMap seems to be used for this. Maybe it helps you.

graker
graker

Hi MarSch,

If I understood the code correctly, the fieldMap widget uses pretty much the same approach I do: add lat&lng inputs to the model form directly and then fill them with values from widget by JS code. The code in your plugin looks much cleaner than mine though, so I'll check it out more thoroughly. Thanks!

MarSch
MarSch

It is not my plugin... but i thought this could maybe help you ;)

graker
graker

Thanks nonetheless :)

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.