Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Merge pull request #104 from AydinHassan/datetime-local-support
Browse files Browse the repository at this point in the history
datetime-local support
  • Loading branch information
adamwathan committed Sep 20, 2016
2 parents 8056cea + 93524cf commit e6a1dfe
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/AdamWathan/Form/Elements/DateTimeLocal.php
@@ -0,0 +1,19 @@
<?php

namespace AdamWathan\Form\Elements;

class DateTimeLocal extends Text
{
protected $attributes = [
'type' => 'datetime-local',
];

public function value($value)
{
if ($value instanceof \DateTime) {
$value = $value->format('Y-m-d\TH:i');
}

return parent::value($value);
}
}
12 changes: 12 additions & 0 deletions src/AdamWathan/Form/FormBuilder.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/BindingTest.php
Expand Up @@ -59,6 +59,16 @@ public function testBindDate()
$this->assertEquals($expected, $result);
}

public function testBindDateTimeLocal()
{
$object = $this->getStubObject();
$this->form->bind($object);

$expected = '<input type="datetime-local" name="date_and_time_of_birth" value="1985-05-06T16:39">';
$result = (string) $this->form->dateTimeLocal('date_and_time_of_birth');
$this->assertEquals($expected, $result);
}

public function testBindSelect()
{
$object = $this->getStubObject();
Expand Down Expand Up @@ -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';
Expand Down
27 changes: 27 additions & 0 deletions tests/DateTimeLocalTest.php
@@ -0,0 +1,27 @@
<?php

use AdamWathan\Form\Elements\DateTimeLocal;

class DateTimeLocalTest extends PHPUnit_Framework_TestCase
{
use InputContractTest;

protected function newTestSubjectInstance($name)
{
return new DateTimeLocal($name);
}

protected function getTestSubjectType()
{
return 'datetime-local';
}

public function testDateTimeValuesAreBoundAsFormattedStrings()
{
$dateTimeLocal = new DateTimeLocal('dob');
$dateTimeLocal->value(new DateTime('12-04-1988 10:33'));

$expected = '<input type="datetime-local" name="dob" value="1988-04-12T10:33">';
$this->assertSame($expected, $dateTimeLocal->render());
}
}
11 changes: 11 additions & 0 deletions tests/FormBuilderTest.php
Expand Up @@ -253,6 +253,17 @@ public function testDate()
$this->assertEquals($expected, $result);
}

public function testDateTimeLocal()
{
$expected = '<input type="datetime-local" name="date_and_time_of_birth">';
$result = (string) $this->form->dateTimeLocal('date_and_time_of_birth');
$this->assertEquals($expected, $result);

$expected = '<input type="datetime-local" name="start_date_and_time">';
$result = (string) $this->form->dateTimeLocal('start_date_and_time');
$this->assertEquals($expected, $result);
}

public function testEmail()
{
$expected = '<input type="email" name="email">';
Expand Down
13 changes: 13 additions & 0 deletions tests/OldInputTest.php
Expand Up @@ -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 = '<input type="datetime-local" name="date_and_time_of_birth" value="1985-05-06T16:39">';
$result = (string) $this->form->dateTimeLocal('date_and_time_of_birth');
$this->assertEquals($expected, $result);
}

public function testRenderEmailWithOldInput()
{
$oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface');
Expand Down

0 comments on commit e6a1dfe

Please sign in to comment.