Skip to content

Commit

Permalink
bug #11342 [2.3][Form] Check if IntlDateFormatter constructor returne…
Browse files Browse the repository at this point in the history
…d a valid object before using it (romainneutron)

This PR was merged into the 2.3 branch.

Discussion
----------

[2.3][Form] Check if IntlDateFormatter constructor returned a valid object before using it

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT

`IntlDateFormatter` constructor [may return false](http://www.php.net/manual/en/intldateformatter.create.php#refsect1-intldateformatter.create-returnvalues). This patches avoids fatal errors in these cases

This PR replaces #11334

Commits
-------

ebf967d [Form] Check if IntlDateFormatter constructor returned a valid object before using it
  • Loading branch information
fabpot committed Jul 23, 2014
2 parents 0b5348e + ebf967d commit 797d814
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Expand Up @@ -152,6 +152,8 @@ public function reverseTransform($value)
* Returns a preconfigured IntlDateFormatter instance
*
* @return \IntlDateFormatter
*
* @throws TransformationFailedException in case the date formatter can not be constructed.
*/
protected function getIntlDateFormatter()
{
Expand All @@ -162,6 +164,12 @@ protected function getIntlDateFormatter()
$pattern = $this->pattern;

$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);

// new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/bug.php?id=66323
if (!$intlDateFormatter) {
throw new TransformationFailedException(intl_get_error_message(), intl_get_error_code());
}

$intlDateFormatter->setLenient(false);

return $intlDateFormatter;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -77,6 +77,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$calendar,
$pattern
);

// new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/bug.php?id=66323
if (!$formatter) {
throw new InvalidOptionsException(intl_get_error_message(), intl_get_error_code());
}

$formatter->setLenient(false);

if ('choice' === $options['widget']) {
Expand Down

0 comments on commit 797d814

Please sign in to comment.