Browse files

Implement empty options for datetime selects.

  • Loading branch information...
1 parent 0601ea1 commit e4121d27bce67663f020f6b0e0c0bcfb9d1ac931 @markstory markstory committed Jan 21, 2014
Showing with 36 additions and 4 deletions.
  1. +14 −2 src/View/Input/DateTime.php
  2. +22 −2 tests/TestCase/View/Input/DateTimeTest.php
View
16 src/View/Input/DateTime.php
@@ -90,7 +90,7 @@ public function render(array $data) {
'name' => 'data',
'empty' => false,
'disabled' => null,
- 'val' => new \DateTime(),
+ 'val' => null,
'year' => [],
'month' => [
'names' => false,
@@ -111,7 +111,13 @@ public function render(array $data) {
$method = $select . 'Select';
$data[$select]['name'] = $data['name'] . "[" . $select . "]";
$data[$select]['val'] = $selected[$select];
- $data[$select]['empty'] = $data['empty'];
+
+ if (is_bool($data['empty'])) {
+ $data[$select]['empty'] = $data['empty'];
+ }
+ if (isset($data['empty'][$select])) {
+ $data[$select]['empty'] = $data['empty'][$select];
+ }
$data[$select]['disabled'] = $data['disabled'];
$data[$select] += $data[$select];
$templateOptions[$select] = $this->{$method}($data[$select]);
@@ -130,6 +136,12 @@ public function render(array $data) {
* @return array
*/
protected function _deconstuctDate($value) {
+ if (empty($value)) {
+ return [
+ 'year' => '', 'month' => '', 'day' => '',
+ 'hour' => '', 'minute' => '', 'second' => ''
+ ];
+ }
if (is_string($value)) {
$date = new \DateTime($value);
} elseif (is_int($value)) {
View
24 tests/TestCase/View/Input/DateTimeTest.php
@@ -84,8 +84,29 @@ public function testRenderSelected($selected) {
$this->assertContains('<option value="45" selected="selected">45</option>', $result);
}
+/**
+ * Test rendering widgets with empty values.
+ *
+ * @retun void
+ */
public function testRenderEmptyValues() {
- $this->markTestIncomplete();
+ $result = $this->DateTime->render([
+ 'empty' => [
+ 'year' => 'YEAR',
+ 'month' => 'MONTH',
+ 'day' => 'DAY',
+ 'hour' => 'HOUR',
+ 'minute' => 'MINUTE',
+ 'second' => 'SECOND',
+ 'meridian' => 'MERIDIAN',
+ ]
+ ]);
+ $this->assertContains('<option value="" selected="selected">YEAR</option>', $result);
+ $this->assertContains('<option value="" selected="selected">MONTH</option>', $result);
+ $this->assertContains('<option value="" selected="selected">DAY</option>', $result);
+ $this->assertContains('<option value="" selected="selected">HOUR</option>', $result);
+ $this->assertContains('<option value="" selected="selected">MINUTE</option>', $result);
+ $this->assertContains('<option value="" selected="selected">SECOND</option>', $result);
}
public function testRenderYearWidget() {
@@ -144,7 +165,6 @@ public function testRenderMeridianWidget() {
$this->markTestIncomplete();
}
-
/**
* testGenerateNumbers
*

0 comments on commit e4121d2

Please sign in to comment.