diff --git a/src/AdamWathan/Form/Elements/DateTimeLocal.php b/src/AdamWathan/Form/Elements/DateTimeLocal.php new file mode 100644 index 0000000..94c076e --- /dev/null +++ b/src/AdamWathan/Form/Elements/DateTimeLocal.php @@ -0,0 +1,19 @@ + 'datetime-local', + ]; + + public function value($value) + { + if ($value instanceof \DateTime) { + $value = $value->format('Y-m-d\TH:i'); + } + + return parent::value($value); + } +} diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index ab97569..7b86528 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -6,6 +6,7 @@ use AdamWathan\Form\Elements\Button; use AdamWathan\Form\Elements\Checkbox; use AdamWathan\Form\Elements\Date; +use AdamWathan\Form\Elements\DateTimeLocal; use AdamWathan\Form\Elements\Email; use AdamWathan\Form\Elements\File; use AdamWathan\Form\Elements\FormOpen; @@ -89,6 +90,17 @@ public function date($name) return $date; } + public function dateTimeLocal($name) + { + $date = new DateTimeLocal($name); + + if (!is_null($value = $this->getValueFor($name))) { + $date->value($value); + } + + return $date; + } + public function email($name) { $email = new Email($name); diff --git a/tests/BindingTest.php b/tests/BindingTest.php index 565ac01..d350e83 100644 --- a/tests/BindingTest.php +++ b/tests/BindingTest.php @@ -59,6 +59,16 @@ public function testBindDate() $this->assertEquals($expected, $result); } + public function testBindDateTimeLocal() + { + $object = $this->getStubObject(); + $this->form->bind($object); + + $expected = ''; + $result = (string) $this->form->dateTimeLocal('date_and_time_of_birth'); + $this->assertEquals($expected, $result); + } + public function testBindSelect() { $object = $this->getStubObject(); @@ -433,6 +443,7 @@ private function getStubObject() $obj->first_name = 'John'; $obj->last_name = 'Doe'; $obj->date_of_birth = new \DateTime('1985-05-06'); + $obj->date_and_time_of_birth = new \DateTime('1985-05-06 16:39'); $obj->gender = 'male'; $obj->terms = 'agree'; $obj->color = 'green'; diff --git a/tests/DateTimeLocalTest.php b/tests/DateTimeLocalTest.php new file mode 100644 index 0000000..238a966 --- /dev/null +++ b/tests/DateTimeLocalTest.php @@ -0,0 +1,27 @@ +value(new DateTime('12-04-1988 10:33')); + + $expected = ''; + $this->assertSame($expected, $dateTimeLocal->render()); + } +} diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index d32b934..18bb12f 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -253,6 +253,17 @@ public function testDate() $this->assertEquals($expected, $result); } + public function testDateTimeLocal() + { + $expected = ''; + $result = (string) $this->form->dateTimeLocal('date_and_time_of_birth'); + $this->assertEquals($expected, $result); + + $expected = ''; + $result = (string) $this->form->dateTimeLocal('start_date_and_time'); + $this->assertEquals($expected, $result); + } + public function testEmail() { $expected = ''; diff --git a/tests/OldInputTest.php b/tests/OldInputTest.php index 914e995..2d7cdc6 100644 --- a/tests/OldInputTest.php +++ b/tests/OldInputTest.php @@ -142,6 +142,19 @@ public function testRenderDateWithOldInput() $this->assertEquals($expected, $result); } + public function testRenderDateTimeLocalWithOldInput() + { + $oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface'); + $oldInput->shouldReceive('hasOldInput')->andReturn(true); + $oldInput->shouldReceive('getOldInput')->with('date_and_time_of_birth')->andReturn('1985-05-06T16:39'); + + $this->form->setOldInputProvider($oldInput); + + $expected = ''; + $result = (string) $this->form->dateTimeLocal('date_and_time_of_birth'); + $this->assertEquals($expected, $result); + } + public function testRenderEmailWithOldInput() { $oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface');