Skip to content

Commit

Permalink
add support for <input type="datetime-local">, per #4
Browse files Browse the repository at this point in the history
  • Loading branch information
mindplay-dk committed Jan 24, 2018
1 parent 2d6a2a8 commit a45428d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/Fields/DateTimeLocalField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace mindplay\kissform\Fields;

use mindplay\kissform\InputModel;

/**
* Date/time field-type for string-based input using HTML5 `<input type="datetime-local">`.
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local
*/
class DateTimeLocalField extends DateTimeField
{
const HTML5_FORMAT = "Y-m-d\\TH:i";

/**
* @param string $name field name
* @param string $timezone timezone name
* @param array $attrs map of HTML attribtues to apply
*/
public function __construct($name, $timezone, $attrs = [])
{
$attrs += [
"type" => "datetime-local",
];

parent::__construct($name, $timezone, self::HTML5_FORMAT, $attrs);
}
}
19 changes: 17 additions & 2 deletions tests/unit/FieldRenderCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use mindplay\kissform\Fields\CheckboxField;
use mindplay\kissform\Fields\DateSelectField;
use mindplay\kissform\Fields\DateTimeField;
use mindplay\kissform\Fields\DateTimeLocalField;
use mindplay\kissform\Fields\EmailField;
use mindplay\kissform\Fields\FloatField;
use mindplay\kissform\Fields\HiddenField;
Expand Down Expand Up @@ -251,8 +252,22 @@ public function renderDateTimeField(UnitTester $I)
$field = new DateTimeField('value', 'Europe/Copenhagen', 'Y-m-d H:i:s', ['readonly' => true]);
$field->setValue($form->model, 173919600);

$I->assertSame('<input class="form-control" id="form-value" name="value" readonly type="text" value="1975-07-07 00:00:00"/>',
$form->render($field));
$I->assertSame(
'<input class="form-control" id="form-value" name="value" readonly type="text" value="1975-07-07 00:00:00"/>',
$form->render($field)
);
}

public function renderDateTimeLocalField(UnitTester $I)
{
$form = new InputRenderer();
$field = new DateTimeLocalField('value', 'Europe/Copenhagen', ['readonly' => true]);
$field->setValue($form->model, 173919600);

$I->assertSame(
'<input class="form-control" id="form-value" name="value" readonly type="datetime-local" value="1975-07-07T00:00"/>',
$form->render($field)
);
}

public function renderDateSelector(UnitTester $I)
Expand Down

0 comments on commit a45428d

Please sign in to comment.