Yii 1.1: states

Creates a dropdown list of states from the US, US Territories and Canada
5 followers

Creates a dropdown list of states from the US, US Territories and Canada

GitHub

Requirements

  • Yii 1.1.x

Install

  • Extract to extensions directory
  • Add following to the import section of the main config
  • 'application.extensions.states.*'

Usage

$options = array(
  "name" => "thestates",
  "seperateCountries" => true,
  "selected" => "VA",
);
$htmlOptions = array(
  "prompt" => "Please select a state",
);
 
echo States::render(array("US","canada","us territories"), $options, $htmlOptions);

Active Form

$form->dropDownList($address, 'state', My_States::getStates(array("US"), true), 
       array('prompt' => 'State', 'class' => 'abc', 'style' => "width:75px;")),

Total 9 comments

#12579 report it
cfletcher1856 at 2013/03/29 12:50pm
Update

Thanks Alan, your changes were added to 1.1

#12417 report it
Alan Lobo at 2013/03/19 07:51am
ability to use the states in CActiveForm widgets

Hi cfletcher1856,

I made the following changes to this extension inorder to 1. allow me to use the states with CActiveForm widgets 2. allow me to generate a drop down of the short-names of the states.

class My_States extends States
{
    public static function getStates(array $countries, $shortName = FALSE)
    {
        $return = array();
        if (in_array('US', $countries)) $return += self::$us;
        if (in_array('CANADA', $countries)) $return += self::$canada;
        if (in_array('US TERRITORIES', $countries)) $return += self::$us_territories;
 
        if ($shortName)
            foreach ($return as $key => &$value)
                $value = $key;
        return $return;
    }
}

and

- private static $us = array(
+ protected static $us = array(
...
- private static $us_territories = array(
+ protected static $us_territories = array(
...
- private static $canada = array(
+ protected static $canada = array(

With this, I could use the function in a way consistent with the other form element calls:

$form->dropDownList($address, 'state', My_States::getStates(array("US"), true), array('prompt' => 'State', 'class' => 'abc', 'style' => "width:75px;")),

You could consider adding this to the extension. Thanks, Alan

#9828 report it
cfletcher1856 at 2012/09/13 04:52pm
Form help

@oneflatfoot, as of now this just extends the CApplicationComponent and returns CHtml::dropDownList() You can put it in anyform and just deal with the data in the controller. The data will be sent in the post.

Let me know if you need anymore help.

#9826 report it
oneflatfoot at 2012/09/13 03:16pm
Form help

Sorry, but I am a severe noob. How do I put this in my form? Is it a $form->dropDownList? If so, does the usage code go in the CHtml::listData section? Thanks.

#7835 report it
gork4life at 2012/04/21 12:14pm
Thanks for the extension

My question is do I store this in database as varchar or text, enum?

#6773 report it
Yiimaps at 2012/02/03 08:31pm
Thank you

Got it to work :-) It was simple . Thank you for the quick response and awesome extension..

#6745 report it
cfletcher1856 at 2012/02/02 10:19am
Usage

Hi Ruaan,

Did you not see the install and usage sections above?

What problems are you running into?

@dungdeveloper, I'm glad that the extension helped you out.

#6733 report it
Yiimaps at 2012/02/01 06:53pm
help

How do I use this extension ?

#5759 report it
dungdeveloper at 2011/11/10 12:36am
thank you

Thanks for your extension. It helps me alot!

Leave a comment

Please to leave your comment.

Create extension