Skip to content

Commit

Permalink
[Form] fixed bug that prevented setLocale from working
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Turner authored and fabpot committed Sep 25, 2010
1 parent eb942c8 commit 86b6aff
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions src/Symfony/Component/Form/DateField.php
Expand Up @@ -96,11 +96,7 @@ protected function configure()
$this->addOption('widget', self::CHOICE, self::$widgets);
$this->addOption('pattern');

$this->formatter = new \IntlDateFormatter(
$this->locale,
self::$intlFormats[$this->getOption('format')],
\IntlDateFormatter::NONE
);
$this->initFormatter();

$transformers = array();

Expand Down Expand Up @@ -140,15 +136,7 @@ protected function configure()

$this->setFieldMode(self::GROUP);

$this->add(new ChoiceField('year', array(
'choices' => $this->generatePaddedChoices($this->getOption('years'), 4),
)));
$this->add(new ChoiceField('month', array(
'choices' => $this->generateMonthChoices($this->getOption('months')),
)));
$this->add(new ChoiceField('day', array(
'choices' => $this->generatePaddedChoices($this->getOption('days'), 2),
)));
$this->addChoiceFields();
}

if (count($transformers) > 0) {
Expand Down Expand Up @@ -239,4 +227,49 @@ public function render(array $attributes = array())
), $pattern);
}
}


/**
* Sets the locale of this field.
*
* @see Localizable
*/
public function setLocale($locale)
{
parent::setLocale($locale);

$this->initFormatter();

if ($this->getOption('widget') === self::CHOICE) {
$this->addChoiceFields();
}
}

/**
* Initializes (or reinitializes) the formatter
*/
protected function initFormatter()
{
$this->formatter = new \IntlDateFormatter(
$this->locale,
self::$intlFormats[$this->getOption('format')],
\IntlDateFormatter::NONE
);
}

/**
* Adds (or replaces if already added) the fields used when widget=CHOICE
*/
protected function addChoiceFields()
{
$this->add(new ChoiceField('year', array(
'choices' => $this->generatePaddedChoices($this->getOption('years'), 4),
)));
$this->add(new ChoiceField('month', array(
'choices' => $this->generateMonthChoices($this->getOption('months')),
)));
$this->add(new ChoiceField('day', array(
'choices' => $this->generatePaddedChoices($this->getOption('days'), 2),
)));
}
}

0 comments on commit 86b6aff

Please sign in to comment.