Skip to content

Commit

Permalink
Fix forms with 24hr datetime inputs being blackholed.
Browse files Browse the repository at this point in the history
Omit the meridian picker from the secured fields if the form has a 24
hour format hour picker.

Refs #5105
  • Loading branch information
markstory committed Nov 10, 2014
1 parent b9355c4 commit 228863f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/View/Widget/DateTimeWidget.php
Expand Up @@ -527,12 +527,22 @@ protected function _generateNumbers($start, $end, $options = []) {
}

/**
* {@inheritDoc}
* Returns a list of fields that need to be secured for this widget.
*
* When the hour picker is in 24hr mode (null or format=24) the meridian
* picker will be omitted.
*
* @param array $data The data to render.
* @return array Array of fields to secure.
*/
public function secureFields(array $data) {
$fields = [];
$hourFormat = isset($data['hour']['format']) ? $data['hour']['format'] : null;
foreach ($this->_selects as $type) {
if ($data[$type] !== false) {
if ($type === 'meridian' && ($hourFormat === null || $hourFormat === 24)) {
continue;
}
if (!isset($data[$type]) || $data[$type] !== false) {
$fields[] = $data['name'] . '[' . $type . ']';
}
}
Expand Down
42 changes: 42 additions & 0 deletions tests/TestCase/View/Widget/DateTimeWidgetTest.php
Expand Up @@ -966,4 +966,46 @@ public function testRenderMeridianWidget() {
$this->assertHtml($expected, $result);
}

/**
* Test that secureFields omits removed selects
*
* @return void
*/
public function testSecureFields() {
$data = [
'name' => 'date',
];
$result = $this->DateTime->secureFields($data);
$expected = [
'date[year]', 'date[month]', 'date[day]',
'date[hour]', 'date[minute]', 'date[second]',
];
$this->assertEquals($expected, $result, 'No meridian on 24hr input');

$data = [
'name' => 'date',
'hour' => ['format' => 24]
];
$result = $this->DateTime->secureFields($data);
$this->assertEquals($expected, $result, 'No meridian on 24hr input');

$data = [
'name' => 'date',
'year' => false,
'month' => false,
'day' => false,
'hour' => [
'format' => 12,
'data-foo' => 'test'
],
'minute' => false,
'second' => false,
];
$result = $this->DateTime->secureFields($data);
$expected = [
'date[hour]', 'date[meridian]'
];
$this->assertEquals($expected, $result);
}

}

0 comments on commit 228863f

Please sign in to comment.