Just thought some of you might like to know...

If you leave your '#options' empty on your target(s) and add '#DANGEROUS_SKIP_CHECK' => TRUE, then Drupal will skip the #options validation that is mentioned in the active select API.txt. If you do this, you MUST do your own validation.

Using this, you can do true country->state->city selection.

At least, that's how things look so far. Look for a new release of cck_address soon, which will contain a country->state selector if you'd like to see an example.

Comments

rconstantine’s picture

Yep, real AJAX works. All that's left for me to upload my changes is to fix a CCK-related issue.

But it looks like this post didn't garner any interest.

Oh, well.

liquidcms’s picture

yup, seems to work.. very nice..

now if someone just new how to send only default options and have the correct one picked on initial page load.

rconstantine’s picture

I did figure out dynamic AJAX default options for targets (and sources) as well. I imagine you don't have problems setting up the default for the source field. For targets, you need to send your activeselect function the correct value via the 'extra' parameter and then make the corresponding entry in your dropdown list the 'selected' one. Only set the first entry as 'selected' if there is no 'extra' value.

Things to watch out for

- if you have a dropdown list of key->value pairs where key!=value, then make sure the value you pass via 'extra' is checked correctly so that 'selected' is set correctly. In my case, I have state abbreviations->state names and was sending (or checking) the wrong one so the for the longest time I wasn't getting the default set correctly. I was either sending the key and checking the value or the other way around.

- at least as a cck module goes, you have to make sure that the value you send to 'extra' is the current value. For example, if you are creating a new content type, there are no initial values for any fields, which is what you'd expect. On preview, both $_POST and $items contain the submitted value and either will do (if I recall correctly). If you're setting 'extra' from either of these, the default value of the target should be okay and reflect the choice you made. You can then submit and the correct values are saved. If you then edit an existing node, $_POST won't have values, but $item will. If you then preview after changing a value, $_POST will have the correct value (the new one) and $items will have the stored value. So you need to grab $_POST in this case. In other words, use an if/else or ?: to make sure the most recent value takes precedence over the stored value and is sent to activeselect via 'extra'. I hope that all makes sense. The same goes for the source, come to think of it. The only difference is that you just put it into #default_value, not 'extra'.

- for some reason, I didn't consistently get a result for 'extra' if I drupal_set_message'd it out, even though it otherwise behaved correctly, so make sure that what you're GOING TO SEND is correct and trust that the function receives the right value(s).

By 'extra' in all of my references above, I may mean 'activeselect_extra' or whatever it's called in the form definition. It's called 'extra' inside your activeselect function.

This will all be in my cck_address module if some cck people can tell me how my newly added column will be propagated to existing instances. That's the only thing hanging me up right now.

liquidcms’s picture

very cool.. thanks for the recipe.. will check it out later this evening...

liquidcms’s picture

well just started to look at it i have no idea what the format for #activeselect_extra is supposed to be... an example of what you set it to would sure be handy :).

rconstantine’s picture

API.txt says that it must be a string and not an array. So in my case, I'm passing it a two character state code (or was it the full state name?). Either way, in my activeselect function, I then lookup the allowed values for the state by using the country (which was passed in via 'string'). These will be passed via AJAX as the #options. I modified the loop in the example from API.txt so that the first element is set to 'selected' only if 'extra' is not set. Otherwise, if $key == $extra, set that one to 'selected'. Does that make sense?

I did get a tip on how to fix my cck issues on my module, so expect to see the update sometime next week.

liquidcms’s picture

ok, i think i see now...

i have been trying to use the categories module activeselect enabled category selector. I have simply snipped out the code they use for their form and have used in in multiple places through my site. But, the callback function i have left untouched and simply used their callback.

I think what you are describing is writing everything from scratch and using extra to pass a "Default value" and then writing your own ajax callback handler function that creates the correct list (with the correct default) to be sent back... and i would guess, that the category module's handler doesn't do any of this.

So, unfortunately i will need to write my own.. but likely not too big a deal.

the "update" you mentioned, is this the country/state cck type you mentioned earlier. I am very interested in this. Can you give me any details?

Is this an international country list? what states (us only, us/canada, world)? Cities? What db do you use? The one that was "donated" to drupal from maxmind?

liquidcms’s picture

oh, i just saw your project page for cck_address. So i guess drupal 5 and US only. Neither of much use to me right now. But at least i will see examples of the ajax stuff and hopefully will be able to build my own international version.

liquidcms’s picture

ok, got it... doing pretty much what you had suggested.. i still use the category modules handler code but pull out their bit about setting selected and use the value for $extra that i send.

I also, in my form create code (which is also from the category module.. i set extra based on the POST value; but the post value for the previous field - since the handler process the first selector.. which builds the children of this category and then sets the selected value.. so the $extra value (i.e. "selected") for select 2 has to be sent with the $source = select 1

thanks again for all your help,

rconstantine’s picture

I just followed the basic outline of API.txt from activeselect and it mentions modifying the your_module_activeselect function to output what you need. It sounds like you figured that out.

As for my cck_address module: I have just added Canadian support and made it simple (I think) to add other countries data. I use Wednesdays as my module update day, so you should see the update posted then. I had hoped that the cck issue I am having would have had a simple solution where I could simply call an existing cck function to take care of it; but it looks like I'll have to write my own function instead. Oh well.

Anyway, I'm glad I could help.

BTW, are you working on a module that you'll release to the community?

liquidcms’s picture

nope.. working on a large drupal site... have used usernode, userpoints, video conversion, custom views integration, cck, ajax and many other bits quite extensively in it - added custom code to most of the modules i am using.. and added a couple add-on modules (to things like userpoints) along the way...

should be almost ready to unveil in a few weeks... stay tuned

rconstantine’s picture

Just uploaded the new release of cck_address yesterday. Find it here: http://drupal.org/project/cck_address. The activeselect stuff should be easy to see, but of course, if you have questions, let me know. I'll be happy to explain anything.

rconstantine’s picture

Status: Active » Closed (fixed)