Skip to content

Commit a0466ae

Browse files
Merge pull request #1032 from romainguerrero/feature/input-filter-format-for-datetime-column
DateTime and Date columns : add input filter format option
2 parents 167cbcf + a738da5 commit a0466ae

5 files changed

Lines changed: 81 additions & 7 deletions

File tree

Grid/Column/DateColumn.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class DateColumn extends DateTimeColumn
2020

2121
protected $fallbackFormat = 'Y-m-d';
2222

23+
protected $fallbackInputFormat = 'Y-m-d';
24+
2325
public function getFilters($source)
2426
{
2527
$parentFilters = parent::getFilters($source);

Grid/Column/DateTimeColumn.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ class DateTimeColumn extends Column
2424

2525
protected $fallbackFormat = 'Y-m-d H:i:s';
2626

27+
protected $inputFormat;
28+
29+
protected $fallbackInputFormat = 'Y-m-d H:i:s';
30+
2731
protected $timezone;
2832

2933
public function __initialize(array $params)
3034
{
3135
parent::__initialize($params);
3236

3337
$this->setFormat($this->getParam('format'));
38+
$this->setInputFormat($this->getParam('inputFormat', $this->fallbackInputFormat));
3439
$this->setOperators($this->getParam('operators', [
3540
self::OPERATOR_EQ,
3641
self::OPERATOR_NEQ,
@@ -56,7 +61,7 @@ public function isQueryValid($query)
5661

5762
protected function isDateTime($query)
5863
{
59-
return strtotime($query) !== false;
64+
return false !== \DateTime::createFromFormat($this->inputFormat, $query);
6065
}
6166

6267
public function getFilters($source)
@@ -65,7 +70,7 @@ public function getFilters($source)
6570

6671
$filters = [];
6772
foreach ($parentFilters as $filter) {
68-
$filters[] = ($filter->getValue() === null) ? $filter : $filter->setValue(new \DateTime($filter->getValue()));
73+
$filters[] = ($filter->getValue() === null) ? $filter : $filter->setValue(\DateTime::createFromFormat($this->inputFormat, $filter->getValue()));
6974
}
7075

7176
return $filters;
@@ -160,6 +165,18 @@ public function getFormat()
160165
return $this->format;
161166
}
162167

168+
public function setInputFormat($inputFormat)
169+
{
170+
$this->inputFormat = $inputFormat;
171+
172+
return $this;
173+
}
174+
175+
public function getInputFormat()
176+
{
177+
return $this->inputFormat;
178+
}
179+
163180
public function getTimezone()
164181
{
165182
return $this->timezone;

Resources/doc/columns_configuration/types/date_column.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Date Column
22
===========
33

4-
The Date Column extends the [DateTime Column](datetime_column.md) and the default fallback format is `Y-m-d`.
4+
The Date Column extends the [DateTime Column](datetime_column.md) and the default fallback format and filter input format are `Y-m-d`.
55

66
With this column, the time part of a datetime value is ignored when you filter the column.
77
So, if you filter the column with the value `2012-04-26` or `2012-04-26 12:23:45` and with the operator `=`, the query will be `date >= '2012-04-26 0:00:00' AND date <= '2012-04-26 23:59:59'`.

Resources/doc/columns_configuration/types/datetime_column.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ See [Column annotation for properties](../annotations/column_annotation_property
1515
|:--:|:--|:--|:--|:--|
1616
|format|string| | |Define this attribute if you want to force the format of the displayed value.<br />(e.g. "Y-m-d H:i:s")|
1717
|timezone|string|System default timezone| |The timezone to use for rendering.<br />(e.g. "Europe/Paris")|
18+
|inputFormat|string|"Y-m-d H:i:s"| |Define this attribute if you want to force the format of the filtered value.<br />(e.g. "Y-m-d H:i:s")|
19+
20+
**Note**: If you want to filter using date input (and not datetime input), you should use the [Date Column](date_column.md) type instead and configure the display format to render the time (e.g. "Y-m-d H:i:s").
1821

1922
## Filter
2023
#### Valid values
@@ -41,4 +44,4 @@ Wrong values are ignored.
4144
|btw|Between exclusive|
4245
|btwe|Between inclusive|
4346
|isNull|Is not defined|
44-
|isNotNull|Is defined|
47+
|isNotNull|Is defined|

Tests/Grid/Column/DateTimeColumnTest.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ public function testGetFormat()
3636
$this->assertEquals($format, $column->getFormat());
3737
}
3838

39+
public function testSetInputFormat()
40+
{
41+
$inputFormat = 'Y-m-d';
42+
43+
$column = new DateTimeColumn();
44+
$column->setInputFormat($inputFormat);
45+
46+
$this->assertAttributeEquals($inputFormat, 'inputFormat', $column);
47+
}
48+
49+
public function testGetInputFormat()
50+
{
51+
$inputFormat = 'Y-m-d';
52+
53+
$column = new DateTimeColumn();
54+
$column->setInputFormat($inputFormat);
55+
56+
$this->assertEquals($inputFormat, $column->getInputFormat());
57+
}
58+
3959
public function testSetTimezone()
4060
{
4161
$timezone = 'UTC';
@@ -98,11 +118,23 @@ public function testRenderCellWithCallback()
98118
public function testFilterWithValue()
99119
{
100120
$column = new DateTimeColumn();
101-
$column->setData(['operator' => Column::OPERATOR_BTW, 'from' => '2017-03-22', 'to' => '2017-03-23']);
121+
$column->setData(['operator' => Column::OPERATOR_BTW, 'from' => '2017-03-22 01:30:00', 'to' => '2017-03-23 19:00:00']);
102122

103123
$this->assertEquals([
104-
new Filter(Column::OPERATOR_GT, new \DateTime('2017-03-22')),
105-
new Filter(Column::OPERATOR_LT, new \DateTime('2017-03-23')),
124+
new Filter(Column::OPERATOR_GT, new \DateTime('2017-03-22 01:30:00')),
125+
new Filter(Column::OPERATOR_LT, new \DateTime('2017-03-23 19:00:00')),
126+
], $column->getFilters('asource'));
127+
}
128+
129+
public function testFilterWithFormattedValue()
130+
{
131+
$column = new DateTimeColumn();
132+
$column->setInputFormat('m/d/Y H-i-s');
133+
$column->setData(['operator' => Column::OPERATOR_BTW, 'from' => '03/22/2017 01-30-00', 'to' => '03/23/2017 19-00-00']);
134+
135+
$this->assertEquals([
136+
new Filter(Column::OPERATOR_GT, new \DateTime('2017-03-22 01:30:00')),
137+
new Filter(Column::OPERATOR_LT, new \DateTime('2017-03-23 19:00:00')),
106138
], $column->getFilters('asource'));
107139
}
108140

@@ -128,11 +160,28 @@ public function testQueryIsInvalid()
128160
$this->assertFalse($column->isQueryValid('foo'));
129161
}
130162

163+
public function testInputFormattedQueryIsValid()
164+
{
165+
$column = new DateTimeColumn();
166+
$column->setInputFormat('m/d/Y H-i-s');
167+
168+
$this->assertTrue($column->isQueryValid('03/22/2017 23-00-00'));
169+
}
170+
171+
public function testInputFormattedQueryIsInvalid()
172+
{
173+
$column = new DateTimeColumn();
174+
$column->setInputFormat('m/d/Y H-i-s');
175+
176+
$this->assertFalse($column->isQueryValid('2017-03-22 23:00:00'));
177+
}
178+
131179
public function testInitializeDefaultParams()
132180
{
133181
$column = new DateTimeColumn();
134182

135183
$this->assertAttributeEquals(null, 'format', $column);
184+
$this->assertAttributeEquals('Y-m-d H:i:s', 'inputFormat', $column);
136185
$this->assertAttributeEquals([
137186
Column::OPERATOR_EQ,
138187
Column::OPERATOR_NEQ,
@@ -152,10 +201,12 @@ public function testInitializeDefaultParams()
152201
public function testInitialize()
153202
{
154203
$format = 'Y-m-d H:i:s';
204+
$inputFormat = 'Y-m-d H:i:s';
155205
$timezone = 'UTC';
156206

157207
$params = [
158208
'format' => $format,
209+
'inputFormat' => $inputFormat,
159210
'operators' => [Column::OPERATOR_LT, Column::OPERATOR_LTE],
160211
'defaultOperator' => Column::OPERATOR_LT,
161212
'timezone' => $timezone,
@@ -164,6 +215,7 @@ public function testInitialize()
164215
$column = new DateTimeColumn($params);
165216

166217
$this->assertAttributeEquals($format, 'format', $column);
218+
$this->assertAttributeEquals($inputFormat, 'inputFormat', $column);
167219
$this->assertAttributeEquals([
168220
Column::OPERATOR_LT, Column::OPERATOR_LTE,
169221
], 'operators', $column);

0 commit comments

Comments
 (0)