root/trunk/lib/widget/ExtendedWidgetFormI18nSelectCountry.class.php

Revision 70, 2.0 kB (checked in by nperriault, 6 months ago)

[1.1] application form post validators

Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * ExtendedWidgetFormI18nSelectCountry represents a country HTML select tag, and
13  * adds some useful options.
14  *
15  * @package    symfony
16  * @subpackage widget
17  */
18 class ExtendedWidgetFormI18nSelectCountry extends sfWidgetFormSelect
19 {
20   /**
21    * Widget configuration
22    *
23    * Available options:
24    *
25    *  * culture:          The culture to use for internationalized strings (required)
26    *  * countries:        An array of country codes to use (ISO 3166)
27    *  * add_label_option: Adds a custom option at the top of the selector, which the value is empty
28    *
29    * @param array $options     An array of options
30    * @param array $attributes  An array of default HTML attributes
31    *
32    * @see sfWidgetFormSelect
33    */
34   protected function configure($options = array(), $attributes = array())
35   {
36     parent::configure($options, $attributes);
37
38     $this->addRequiredOption('culture');
39     $this->addOption('countries');
40     $this->addOption('add_label_option');
41
42     // populate choices with all countries
43     $culture = isset($options['culture']) ? $options['culture'] : 'en';
44
45     $cultureInfo = new sfCultureInfo($culture);
46     $countries = $cultureInfo->getCountries();
47
48     // restrict countries to a sub-set
49     if (isset($options['countries']))
50     {
51       if ($problems = array_diff($options['countries'], array_keys($countries)))
52       {
53         throw new InvalidArgumentException(sprintf('The following countries do not exist: %s.', implode(', ', $problems)));
54       }
55
56       $countries = array_intersect_key($countries, array_flip($options['countries']));
57     }
58
59     asort($countries);
60     
61     if (isset($options['add_label_option']))
62     {
63       $countries = array('' => $options['add_label_option']) + $countries;
64     }
65
66     $this->setOption('choices', $countries);
67   }
68 }
69
Note: See TracBrowser for help on using the browser.