Skip to content

Commit

Permalink
Add DateTimeInterface value support to date fields (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Apr 1, 2024
1 parent 82bbaf1 commit e147cdb
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Field/Base/DateTimeInputField.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Form\Field\Base;

use DateTimeInterface;
use InvalidArgumentException;
use ReflectionClass;
use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesInterface;
Expand Down Expand Up @@ -150,7 +151,9 @@ final protected function generateInput(): string
{
$value = $this->getValue();

if (!is_string($value) && $value !== null) {
if ($value instanceof DateTimeInterface) {
$value = $this->formatDateTime($value);
} elseif (!is_string($value) && $value !== null) {
throw new InvalidArgumentException(
(new ReflectionClass($this))->getShortName() .
' field requires a string or null value.'
Expand All @@ -166,6 +169,8 @@ final protected function generateInput(): string
return Html::input($this->getInputType(), $this->getName(), $value, $inputAttributes)->render();
}

abstract protected function formatDateTime(DateTimeInterface $value): string;

abstract protected function getInputType(): string;

protected function prepareContainerAttributes(array &$attributes): void
Expand Down
6 changes: 6 additions & 0 deletions src/Field/Date.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Form\Field;

use DateTimeInterface;
use Yiisoft\Form\Field\Base\DateTimeInputField;

/**
Expand All @@ -19,4 +20,9 @@ protected function getInputType(): string
{
return 'date';
}

protected function formatDateTime(DateTimeInterface $value): string
{
return $value->format('Y-m-d');
}
}
6 changes: 6 additions & 0 deletions src/Field/DateTime.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Form\Field;

use DateTimeInterface;
use Yiisoft\Form\Field\Base\DateTimeInputField;

/**
Expand All @@ -19,4 +20,9 @@ protected function getInputType(): string
{
return 'datetime';
}

protected function formatDateTime(DateTimeInterface $value): string
{
return $value->format('Y-m-d\TH:i');
}
}
6 changes: 6 additions & 0 deletions src/Field/DateTimeLocal.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Form\Field;

use DateTimeInterface;
use Yiisoft\Form\Field\Base\DateTimeInputField;

/**
Expand All @@ -19,4 +20,9 @@ protected function getInputType(): string
{
return 'datetime-local';
}

protected function formatDateTime(DateTimeInterface $value): string
{
return $value->format('Y-m-d\TH:i');
}
}
6 changes: 6 additions & 0 deletions src/Field/Time.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Form\Field;

use DateTimeInterface;
use Yiisoft\Form\Field\Base\DateTimeInputField;

/**
Expand All @@ -19,4 +20,9 @@ protected function getInputType(): string
{
return 'time';
}

protected function formatDateTime(DateTimeInterface $value): string
{
return $value->format('H:i');
}
}
17 changes: 17 additions & 0 deletions tests/Field/DateTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Form\Tests\Field;

use DateTime;
use PHPUnit\Framework\TestCase;
use Yiisoft\Form\Field\Base\InputData\PureInputData;
use Yiisoft\Form\Field\Date;
Expand Down Expand Up @@ -56,4 +57,20 @@ public function testRange(): void

$this->assertSame($expected, $result);
}

public function testWithDateTimeInterface(): void
{
$result = Date::widget()
->value(new DateTime('1996-12-19'))
->render();

$this->assertSame(
<<<HTML
<div>
<input type="date" value="1996-12-19">
</div>
HTML,
$result,
);
}
}
17 changes: 17 additions & 0 deletions tests/Field/DateTimeLocalTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Form\Tests\Field;

use DateTime;
use PHPUnit\Framework\TestCase;
use Yiisoft\Form\Field\Base\InputData\PureInputData;
use Yiisoft\Form\Field\DateTimeLocal;
Expand Down Expand Up @@ -39,4 +40,20 @@ public function testBase(): void

$this->assertSame($expected, $result);
}

public function testWithDateTimeInterface(): void
{
$value = (new DateTime('1996-12-19'))->setTime(20, 35);

$result = DateTimeLocal::widget()->value($value)->render();

$this->assertSame(
<<<HTML
<div>
<input type="datetime-local" value="1996-12-19T20:35">
</div>
HTML,
$result,
);
}
}
17 changes: 17 additions & 0 deletions tests/Field/DateTimeTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Form\Tests\Field;

use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Yiisoft\Form\Field\Base\InputData\PureInputData;
use Yiisoft\Form\Field\DateTime;
Expand Down Expand Up @@ -39,4 +40,20 @@ public function testBase(): void

$this->assertSame($expected, $result);
}

public function testWithDateTimeInterface(): void
{
$value = (new DateTimeImmutable('1996-12-19'))->setTime(20, 35);

$result = DateTime::widget()->value($value)->render();

$this->assertSame(
<<<HTML
<div>
<input type="datetime" value="1996-12-19T20:35">
</div>
HTML,
$result,
);
}
}
17 changes: 17 additions & 0 deletions tests/Field/TimeTest.php
Expand Up @@ -6,6 +6,7 @@

namespace Yiisoft\Form\Tests\Field;

use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Yiisoft\Form\Field\Time;
use Yiisoft\Form\ThemeContainer;
Expand Down Expand Up @@ -49,4 +50,20 @@ public function testExtended(): void

$this->assertSame($expected, $result);
}

public function testWithDateTimeInterface(): void
{
$value = (new DateTimeImmutable('1996-12-19'))->setTime(20, 35);

$result = Time::widget()->value($value)->render();

$this->assertSame(
<<<HTML
<div>
<input type="time" value="20:35">
</div>
HTML,
$result,
);
}
}
6 changes: 6 additions & 0 deletions tests/Support/StubDateTimeInputField.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Form\Tests\Support;

use DateTimeInterface;
use Yiisoft\Form\Field\Base\DateTimeInputField;

final class StubDateTimeInputField extends DateTimeInputField
Expand All @@ -12,4 +13,9 @@ protected function getInputType(): string
{
return 'datetime';
}

protected function formatDateTime(DateTimeInterface $value): string
{
return $value->format('d.m.Y');
}
}

0 comments on commit e147cdb

Please sign in to comment.