Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e0d86c

Browse files
committed
allow custom/partial date formats
1 parent 24d8135 commit 5e0d86c

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/Symfony/Component/Form/Extension/Core/Type/DateType.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,28 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5151
$timeFormat = \IntlDateFormatter::NONE;
5252
$calendar = \IntlDateFormatter::GREGORIAN;
5353
$pattern = is_string($options['format']) ? $options['format'] : null;
54+
$fields = array('year', 'month', 'day');
5455

5556
if (!in_array($dateFormat, self::$acceptedFormats, true)) {
5657
throw new InvalidOptionsException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom format.');
5758
}
5859

59-
if (null !== $pattern && (false === strpos($pattern, 'y') || false === strpos($pattern, 'M') || false === strpos($pattern, 'd'))) {
60-
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern));
60+
if (null !== $pattern) {
61+
if (false === strpos($pattern, 'y')) {
62+
unset($fields[0]);
63+
}
64+
65+
if (false === strpos($pattern, 'M')) {
66+
unset($fields[1]);
67+
}
68+
69+
if (false === strpos($pattern, 'd')) {
70+
unset($fields[2]);
71+
}
72+
73+
if (0 === count($fields)) {
74+
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" or "d". Its current value is "%s".', $pattern));
75+
}
6176
}
6277

6378
if ('single_text' === $options['widget']) {
@@ -113,7 +128,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
113128
->add('month', self::$widgets[$options['widget']], $monthOptions)
114129
->add('day', self::$widgets[$options['widget']], $dayOptions)
115130
->addViewTransformer(new DateTimeToArrayTransformer(
116-
$options['model_timezone'], $options['view_timezone'], array('year', 'month', 'day')
131+
$options['model_timezone'], $options['view_timezone'], $fields
117132
))
118133
->setAttribute('formatter', $formatter)
119134
;
@@ -129,7 +144,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
129144
));
130145
} elseif ('array' === $options['input']) {
131146
$builder->addModelTransformer(new ReversedTransformer(
132-
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], array('year', 'month', 'day'))
147+
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $fields)
133148
));
134149
}
135150
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
342342
{
343343
$this->factory->create('Symfony\Component\Form\Extension\Core\Type\DateType', null, array(
344344
'months' => array(6, 7),
345-
'format' => 'yy',
345+
'format' => 'wrong',
346346
));
347347
}
348348

0 commit comments

Comments
 (0)