Skip to content

Commit 7006413

Browse files
author
Amal Raghav
committed
Merge pull request symfony#2 from maoueh/date_pattern
Date pattern
2 parents 09449cb + b8eb062 commit 7006413

File tree

4 files changed

+161
-10
lines changed

4 files changed

+161
-10
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
4040
* @throws UnexpectedTypeException If a format is not supported
4141
* @throws UnexpectedTypeException if a timezone is not a string
4242
*/
43-
public function __construct($inputTimezone = null, $outputTimezone = null, $dateFormat = null, $timeFormat = null, $calendar = null, $pattern = null)
43+
public function __construct($inputTimezone = null, $outputTimezone = null, $dateFormat = null, $timeFormat = null, $calendar = \IntlDateFormatter::GREGORIAN, $pattern = null)
4444
{
4545
parent::__construct($inputTimezone, $outputTimezone);
4646

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,39 @@ class DateType extends AbstractType
2828
{
2929
public function buildForm(FormBuilder $builder, array $options)
3030
{
31+
$format = $options['format'];
32+
$pattern = null;
33+
34+
$allowedFormatOptionValues = array(
35+
\IntlDateFormatter::FULL,
36+
\IntlDateFormatter::LONG,
37+
\IntlDateFormatter::MEDIUM,
38+
\IntlDateFormatter::SHORT,
39+
);
40+
41+
// If $format is not in the allowed options, it's considered as the pattern of the formatter if it is a string
42+
if (!in_array($format, $allowedFormatOptionValues, true)) {
43+
if (is_string($format)) {
44+
$defaultOptions = $this->getDefaultOptions($options);
45+
46+
$format = $defaultOptions['format'];
47+
$pattern = $options['format'];
48+
} else {
49+
throw new FormException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom pattern');
50+
}
51+
}
52+
3153
$formatter = new \IntlDateFormatter(
3254
\Locale::getDefault(),
33-
$options['format'],
55+
$format,
3456
\IntlDateFormatter::NONE,
3557
\DateTimeZone::UTC,
3658
\IntlDateFormatter::GREGORIAN,
37-
$options['pattern']
59+
$pattern
3860
);
3961

4062
if ($options['widget'] === 'single-text') {
41-
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $formatter->getPattern()));
63+
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $format, \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $pattern));
4264
} else {
4365
$yearOptions = $monthOptions = $dayOptions = array();
4466
$widget = $options['widget'];
@@ -142,12 +164,6 @@ public function getAllowedOptionValues(array $options)
142164
'text',
143165
'choice',
144166
),
145-
'format' => array(
146-
\IntlDateFormatter::FULL,
147-
\IntlDateFormatter::LONG,
148-
\IntlDateFormatter::MEDIUM,
149-
\IntlDateFormatter::SHORT,
150-
),
151167
);
152168
}
153169

tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ public function testTransform_differentTimezones()
122122

123123
$this->assertEquals($dateTime->format('d.m.Y H:i'), $transformer->transform($input));
124124
}
125+
126+
public function testTransform_differentPatterns()
127+
{
128+
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, 'MM*yyyy*dd HH|mm|ss');
129+
130+
$this->assertEquals('02*2010*03 04|05|06', $transformer->transform($this->dateTime));
131+
}
125132

126133
/**
127134
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
@@ -217,6 +224,13 @@ public function testReverseTransform_differentTimezones()
217224

218225
$this->assertDateTimeEquals($dateTime, $transformer->reverseTransform('03.02.2010 04:05', null));
219226
}
227+
228+
public function testReverseTransform_differentPatterns()
229+
{
230+
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, 'MM*yyyy*dd HH|mm|ss');
231+
232+
$this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('02*2010*03 04|05|06', null));
233+
}
220234

221235
public function testReverseTransform_empty()
222236
{

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

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,116 @@ public function testSubmitFromChoiceEmpty()
178178
$this->assertEquals($text, $form->getClientData());
179179
}
180180

181+
public function testSubmitFromInputDateTimeDifferentPattern()
182+
{
183+
$form = $this->factory->create('date', null, array(
184+
'data_timezone' => 'UTC',
185+
'user_timezone' => 'UTC',
186+
'format' => 'MM*yyyy*dd',
187+
'widget' => 'single-text',
188+
'input' => 'datetime',
189+
));
190+
191+
$form->bind('06*2010*02');
192+
193+
$this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $form->getData());
194+
$this->assertEquals('06*2010*02', $form->getClientData());
195+
}
196+
197+
public function testSubmitFromInputStringDifferentPattern()
198+
{
199+
$form = $this->factory->create('date', null, array(
200+
'data_timezone' => 'UTC',
201+
'user_timezone' => 'UTC',
202+
'format' => 'MM*yyyy*dd',
203+
'widget' => 'single-text',
204+
'input' => 'string',
205+
));
206+
207+
$form->bind('06*2010*02');
208+
209+
$this->assertEquals('2010-06-02', $form->getData());
210+
$this->assertEquals('06*2010*02', $form->getClientData());
211+
}
212+
213+
public function testSubmitFromInputTimestampDifferentPattern()
214+
{
215+
$form = $this->factory->create('date', null, array(
216+
'data_timezone' => 'UTC',
217+
'user_timezone' => 'UTC',
218+
'format' => 'MM*yyyy*dd',
219+
'widget' => 'single-text',
220+
'input' => 'timestamp',
221+
));
222+
223+
$form->bind('06*2010*02');
224+
225+
$dateTime = new \DateTime('2010-06-02 UTC');
226+
227+
$this->assertEquals($dateTime->format('U'), $form->getData());
228+
$this->assertEquals('06*2010*02', $form->getClientData());
229+
}
230+
231+
public function testSubmitFromInputRawDifferentPattern()
232+
{
233+
$form = $this->factory->create('date', null, array(
234+
'data_timezone' => 'UTC',
235+
'user_timezone' => 'UTC',
236+
'format' => 'MM*yyyy*dd',
237+
'widget' => 'single-text',
238+
'input' => 'array',
239+
));
240+
241+
$form->bind('06*2010*02');
242+
243+
$output = array(
244+
'day' => '2',
245+
'month' => '6',
246+
'year' => '2010',
247+
);
248+
249+
$this->assertEquals($output, $form->getData());
250+
$this->assertEquals('06*2010*02', $form->getClientData());
251+
}
252+
253+
/**
254+
* This test is to check that the strings '0', '1', '2', '3' are no accepted
255+
* as valid IntlDateFormatter constants for FULL, LONG, MEDIUM or SHORT respectively.
256+
*/
257+
public function testFormatOptionCustomPatternCollapsingIntlDateFormatterConstant()
258+
{
259+
$form = $this->factory->create('date', null, array(
260+
'format' => '0',
261+
'widget' => 'single-text',
262+
'input' => 'string',
263+
));
264+
265+
$form->setData('2010-06-02');
266+
267+
// This would be what would be outputed if '0' was mistaken for \IntlDateFormatter::FULL
268+
$this->assertNotEquals('Mittwoch, 02. Juni 2010', $form->getClientData());
269+
}
270+
271+
/**
272+
* @expectedException Symfony\Component\Form\Exception\FormException
273+
*/
274+
public function testValidateFormatOptionGivenWrongConstants()
275+
{
276+
$form = $this->factory->create('date', null, array(
277+
'format' => 105,
278+
));
279+
}
280+
281+
/**
282+
* @expectedException Symfony\Component\Form\Exception\FormException
283+
*/
284+
public function testValidateFormatOptionGivenArrayValue()
285+
{
286+
$form = $this->factory->create('date', null, array(
287+
'format' => array(),
288+
));
289+
}
290+
181291
public function testSetData_differentTimezones()
182292
{
183293
$form = $this->factory->create('date', null, array(
@@ -478,6 +588,17 @@ public function testPassDatePatternToView()
478588

479589
$this->assertSame('{{ day }}.{{ month }}.{{ year }}', $view->get('date_pattern'));
480590
}
591+
592+
public function testPassDatePatternToViewDifferentPattern()
593+
{
594+
$form = $this->factory->create('date', null, array(
595+
'format' => 'MM*yyyy*dd'
596+
));
597+
598+
$view = $form->createView();
599+
600+
$this->assertSame('{{ month }}*{{ year }}*{{ day }}', $view->get('date_pattern'));
601+
}
481602

482603
public function testDontPassDatePatternIfText()
483604
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy