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

Skip to content

Commit c6ef137

Browse files
author
dFayet
committed
Add new Form WeekType
1 parent 19811b8 commit c6ef137

File tree

10 files changed

+342
-11
lines changed

10 files changed

+342
-11
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,17 @@
255255
{{ block('form_widget_simple') }}
256256
{%- endblock color_widget -%}
257257

258+
{%- block week_widget -%}
259+
{%- if widget == 'single_text' -%}
260+
{{ block('form_widget_simple') }}
261+
{%- else -%}
262+
{%- set vars = widget == 'text' ? { 'attr': { 'size': 1 }} : {} -%}
263+
<div {{ block('widget_container_attributes') }}>
264+
{{ form_widget(form.year, vars) }}-{{ form_widget(form.week, vars) }}
265+
</div>
266+
{%- endif -%}
267+
{%- endblock week_widget -%}
268+
258269
{# Labels #}
259270

260271
{%- block form_label -%}

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,6 +2719,21 @@ public function testColor()
27192719
[@name="name"]
27202720
[@class="my&class form-control"]
27212721
[@value="#0000ff"]
2722+
'
2723+
);
2724+
}
2725+
2726+
public function testWeek()
2727+
{
2728+
$form = $this->factory->createNamed('holidays', 'Symfony\Component\Form\Extension\Core\Type\WeekType', '1970-W01');
2729+
2730+
$this->assertWidgetMatchesXpath($form->createView(), ['attr' => ['class' => 'my&class']],
2731+
'/input
2732+
[@type="week"]
2733+
[@name="holidays"]
2734+
[@class="my&class form-control"]
2735+
[@value="1970-W01"]
2736+
[not(@maxlength)]
27222737
'
27232738
);
27242739
}

src/Symfony/Bridge/Twig/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"symfony/asset": "^3.4|^4.0|^5.0",
2525
"symfony/dependency-injection": "^3.4|^4.0|^5.0",
2626
"symfony/finder": "^3.4|^4.0|^5.0",
27-
"symfony/form": "^4.3|^5.0",
27+
"symfony/form": "^4.4|^5.0",
2828
"symfony/http-foundation": "^4.3|^5.0",
2929
"symfony/http-kernel": "^3.4|^4.0|^5.0",
3030
"symfony/mime": "^4.3|^5.0",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php if ($widget == 'single_text'): ?>
2+
<?php echo $view['form']->block($form, 'form_widget_simple', ['type' => isset($type) ? $type : 'week']); ?>
3+
<?php else: ?>
4+
<?php $vars = $widget == 'text' ? ['attr' => ['size' => 1]] : [] ?>
5+
<div <?php echo $view['form']->block($form, 'widget_container_attributes') ?>>
6+
<?php
7+
// There should be no spaces between the colons and the widgets, that's why
8+
// this block is written in a single PHP tag
9+
echo $view['form']->widget($form['year'], $vars);
10+
echo '-';
11+
echo $view['form']->widget($form['week'], $vars);
12+
?>
13+
</div>
14+
<?php endif ?>

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
* add new `WeekType`
7+
48
4.3.0
59
-----
610

src/Symfony/Component/Form/Extension/Core/CoreExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ protected function loadTypes()
8383
new Type\CurrencyType(),
8484
new Type\TelType(),
8585
new Type\ColorType(),
86+
new Type\WeekType(),
8687
];
8788
}
8889

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function transform($dateTime)
5858
return array_intersect_key([
5959
'year' => '',
6060
'month' => '',
61+
'week' => '',
6162
'day' => '',
6263
'hour' => '',
6364
'minute' => '',
@@ -80,6 +81,7 @@ public function transform($dateTime)
8081
$result = array_intersect_key([
8182
'year' => $dateTime->format('Y'),
8283
'month' => $dateTime->format('m'),
84+
'week' => $dateTime->format('W'),
8385
'day' => $dateTime->format('d'),
8486
'hour' => $dateTime->format('H'),
8587
'minute' => $dateTime->format('i'),
@@ -146,6 +148,16 @@ public function reverseTransform($value)
146148
throw new TransformationFailedException('This year is invalid');
147149
}
148150

151+
if (isset($value['week'])) {
152+
if (0 !== strpos($value['week'], 'W') || !ctype_digit($weekNumber = ltrim($value['week'], 'W'))) {
153+
throw new TransformationFailedException('This week is invalid');
154+
}
155+
156+
if (date('W', strtotime('28th December '.$value['year'] ?? '1970')) < $weekNumber) {
157+
throw new TransformationFailedException(sprintf('Week No. %d does not exists for year %d', $weekNumber, $value['year'] ?? '1970'));
158+
}
159+
}
160+
149161
if (!empty($value['month']) && !empty($value['day']) && !empty($value['year']) && false === checkdate($value['month'], $value['day'], $value['year'])) {
150162
throw new TransformationFailedException('This is an invalid date');
151163
}
@@ -163,15 +175,24 @@ public function reverseTransform($value)
163175
}
164176

165177
try {
166-
$dateTime = new \DateTime(sprintf(
167-
'%s-%s-%s %s:%s:%s',
168-
empty($value['year']) ? '1970' : $value['year'],
169-
empty($value['month']) ? '1' : $value['month'],
170-
empty($value['day']) ? '1' : $value['day'],
171-
empty($value['hour']) ? '0' : $value['hour'],
172-
empty($value['minute']) ? '0' : $value['minute'],
173-
empty($value['second']) ? '0' : $value['second']
174-
),
178+
if (\in_array('week', $this->fields)) {
179+
$date = sprintf(
180+
'%s-%s',
181+
empty($value['year']) ? '1970' : $value['year'],
182+
empty($value['week']) ? 'W01' : $value['week']
183+
);
184+
} else {
185+
$date = sprintf(
186+
'%s-%s-%s %s:%s:%s',
187+
empty($value['year']) ? '1970' : $value['year'],
188+
empty($value['month']) ? '1' : $value['month'],
189+
empty($value['day']) ? '1' : $value['day'],
190+
empty($value['hour']) ? '0' : $value['hour'],
191+
empty($value['minute']) ? '0' : $value['minute'],
192+
empty($value['second']) ? '0' : $value['second']
193+
);
194+
}
195+
$dateTime = new \DateTime($date,
175196
new \DateTimeZone($this->outputTimezone)
176197
);
177198

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,28 @@ public function reverseTransform($value)
117117
}
118118

119119
$outputTz = new \DateTimeZone($this->outputTimezone);
120-
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);
120+
121+
if ('Y-\\WW|' === $this->parseFormat) {
122+
if (0 === preg_match('/^\d{4}\-W\d{2}$/', $value)) {
123+
throw new TransformationFailedException('Given data does not follow the date format "Y-\WW"');
124+
}
125+
126+
$weekData = explode('-W', $value);
127+
128+
// The 28th December is always in the last week of the year
129+
if (date('W', strtotime('28th December '.$weekData[0])) < $weekData[1]) {
130+
throw new TransformationFailedException(sprintf('Week No. %d does not exists for year %d', $weekData[1], $weekData[0]));
131+
}
132+
133+
$dateTime = (new \DateTime())->setISODate(...$weekData);
134+
135+
// For when the first day of the first week is in the previous year
136+
if ($dateTime->format('Y') < $weekData[0]) {
137+
$dateTime->setDate($weekData[0], '01', '01');
138+
}
139+
} else {
140+
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);
141+
}
121142

122143
$lastErrors = \DateTime::getLastErrors();
123144

0 commit comments

Comments
 (0)