Skip to content

Commit

Permalink
[Form] Changed to a CreateException when the 'format' option is invalid
Browse files Browse the repository at this point in the history
 * Updated DateTypeTest also
  • Loading branch information
maoueh committed Jun 3, 2011
1 parent 0045ffe commit 436cb95
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\Exception\CreationException;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Extension\Core\ChoiceList\PaddedChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\MonthChoiceList;
use Symfony\Component\Form\FormView;
Expand All @@ -29,26 +31,26 @@ public function buildForm(FormBuilder $builder, array $options)
{
$format = $options['format'];
$pattern = null;

$allowedFormatOptionValues = array(
\IntlDateFormatter::FULL,
\IntlDateFormatter::LONG,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::SHORT,
);

// If $format is not in the allowed options, it's considered as the pattern of the formatter if it is a string
if (!in_array($format, $allowedFormatOptionValues, true)) {
if (is_string($format)) {
$defaultOptions = $this->getDefaultOptions($options);

$format = $defaultOptions['format'];
$pattern = $options['format'];
} else {
throw new FormException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom pattern');
throw new CreationException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom pattern');
}
}

$formatter = new \IntlDateFormatter(
\Locale::getDefault(),
$format,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function testFormatOptionCustomPatternCollapsingIntlDateFormatterConstant
}

/**
* @expectedException Symfony\Component\Form\Exception\FormException
* @expectedException Symfony\Component\Form\Exception\CreationException
*/
public function testValidateFormatOptionGivenWrongConstants()
{
Expand All @@ -279,7 +279,7 @@ public function testValidateFormatOptionGivenWrongConstants()
}

/**
* @expectedException Symfony\Component\Form\Exception\FormException
* @expectedException Symfony\Component\Form\Exception\CreationException
*/
public function testValidateFormatOptionGivenArrayValue()
{
Expand Down

0 comments on commit 436cb95

Please sign in to comment.