Wondering what the best "Yii way" to do this would be

I need to add a feature to a site where users can select the US state(s) and region(s) within those states that they provide professional services in.

The client wants the user to first pick a state, and then be able to select multiple pre-determined regions within that state.

For example, they could first select California (maybe from a drop-down). Then, they are shown the regions in California (Northern California, Bay Area, Southern California, etc), and they can pick which of those regions that they provide service (maybe using checkboxes or multi-select list). Then, they can pick Texas, select the regions in Texas, and so on with as many states/regions as they want.

Any tips for the most efficient/user-friendly way to go about this?


You may want to check this wiki: Creating a dependent dropdown :)

Two tables "states" and "state_regions" (KEY state_id).

I would load regions via Ajax depending on selected state(s).

You can use a drop down to select the state and then vía ajax load a checkboxlist, user will select multiple "cities" and you send all via post i can help you if you want

I have something similar in a website I’m developing. My tips and recommendations:

  • First, you’ll need the data stored somewhere. Probably you’ll store the data in your DB, as already commented above me.
  • I would have packed the entire thing in a module on its own. Some Geo stuff module. By your description above, this module would have contained:[list]
  • Model files to load the DB tables data. This is useful for validation of data inputted by clients. You could fire something like


to validate user input.

I also put in the model files methods such as ‘drop down generator’ which returns a (translated) ‘options’ array suitable for usage directly as the dropdown options in the form. In the methods that generate such dropdowns I use caching to speed things up - the geo data hardly changes so using caching is very good practice to lower resource usage and improve site performance.

  • Controller class that processes the AJAX requests for the dependent drop downs.
  • Module file. I typically stuff it with logic that is not suitable in the controller or model files, if you have any such logic.