diff --git a/README.md b/README.md
index c635b16ea..b61b41ee0 100644
--- a/README.md
+++ b/README.md
@@ -129,6 +129,7 @@ The following documentation describes how to use widgets with PHP:
- [Checkbox](docs/checkbox.md)
- [CheckboxList](docs/checkboxlist.md)
+- [Date](docs/date.md)
- [DateTime](docs/datetime.md)
- [DateTimeLocal](docs/datetimelocal.md)
- [Radio](docs/radio.md)
diff --git a/docs/date.md b/docs/date.md
new file mode 100644
index 000000000..8f1e852bf
--- /dev/null
+++ b/docs/date.md
@@ -0,0 +1,74 @@
+# Date widget
+
+[Date](https://www.w3.org/TR/2012/WD-html-markup-20120329/input.date.html#input.date) an input element for setting the element’s value to a string representing a date.
+
+## Usage
+
+```php
+
+
+= Form::widget()->action('widgets')->csrf($csrf)->begin(); ?>
+ = Date::widget()->config($data, 'dateOfBirth')->render(); ?>
+
+ = Field::widget()->submitButton(['class' => 'button is-block is-info is-fullwidth', 'value' => 'Save']); ?>
+= Form::end(); ?>
+```
+
+That would generate the following code:
+
+```html
+
+```
+
+### `Common` methods:
+
+Method | Description | Default
+-------|-------------|---------
+`autofocus(bool $value = true)` | Sets the autofocus attribute | `false`
+`charset(string $value)` | Sets the charset attribute | `UTF-8`
+`config(FormModelInterface $formModel, string $attribute, array $attributes = [])` | Configures the widget. |
+`disabled(bool $value = true)` | Sets the disabled attribute | `false`
+`form(string $value)` | Sets the form attribute | `''`
+`id(string $value)` | Sets the id attribute | `''`
+`min(?string $value)` | The earliest acceptable date | `''`
+`max(?string $value)` | The latest acceptable date | `''`
+`readonly()` | Sets the readonly attribute | `false`
+`required(bool $value = true)` | Sets the required attribute | `false`
+`tabIndex(int $value = 0)` | Sets the tabindex attribute | `0`
diff --git a/src/Widget/Date.php b/src/Widget/Date.php
index 39eab0c0f..96d46aa35 100644
--- a/src/Widget/Date.php
+++ b/src/Widget/Date.php
@@ -36,8 +36,8 @@ protected function run(): string
/** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.date.html#input.date.attrs.value */
$value = HtmlForm::getAttributeValue($new->getFormModel(), $new->attribute);
- if (!is_string($value)) {
- throw new InvalidArgumentException('Date widget requires a string value.');
+ if (!is_string($value) && null !== $value) {
+ throw new InvalidArgumentException('Date widget requires a string or null value.');
}
return Input::tag()
@@ -45,7 +45,7 @@ protected function run(): string
->attributes($new->attributes)
->id($new->getId())
->name(HtmlForm::getInputName($new->getFormModel(), $new->attribute))
- ->value($value)
+ ->value($value === '' ? null : $value)
->render();
}
}
diff --git a/tests/Widget/DateTest.php b/tests/Widget/DateTest.php
index 44245db9f..928977c45 100644
--- a/tests/Widget/DateTest.php
+++ b/tests/Widget/DateTest.php
@@ -18,18 +18,25 @@ final class DateTest extends TestCase
public function testRender(): void
{
$this->assertSame(
- '',
- Date::widget()->config($this->formModel, 'string')->render(),
+ '',
+ Date::widget()->config($this->formModel, 'toDate')->render(),
);
}
public function testValue(): void
{
// string '2021-09-18'
- $this->formModel->setAttribute('string', '2021-09-18');
+ $this->formModel->setAttribute('toDate', '2021-09-18');
$this->assertSame(
- '',
- Date::widget()->config($this->formModel, 'string')->render(),
+ '',
+ Date::widget()->config($this->formModel, 'toDate')->render(),
+ );
+
+ // value null
+ $this->formModel->setAttribute('toDate', null);
+ $this->assertSame(
+ '',
+ Date::widget()->config($this->formModel, 'toDate')->render(),
);
}
@@ -37,7 +44,7 @@ public function testValueException(): void
{
$this->formModel->setAttribute('array', []);
$this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Date widget requires a string value.');
+ $this->expectExceptionMessage('Date widget requires a string or null value.');
Date::widget()->config($this->formModel, 'array')->render();
}
diff --git a/tests/Widget/FieldDateTest.php b/tests/Widget/FieldDateTest.php
index 7cc0b29ad..59d2e3f41 100644
--- a/tests/Widget/FieldDateTest.php
+++ b/tests/Widget/FieldDateTest.php
@@ -23,7 +23,7 @@ public function testRender(): void
$expected = <<
-
+
HTML;
$this->assertEqualsWithoutLE(
@@ -35,13 +35,26 @@ public function testRender(): void
public function testValue(): void
{
// string '2021-09-18'
+ $this->formModel->setAttribute('toDate', '2021-09-18');
$expected = <<
-
+
+
+ HTML;
+ $this->assertEqualsWithoutLE(
+ $expected,
+ Field::widget()->config($this->formModel, 'toDate')->date()->render(),
+ );
+
+ // value null
+ $this->formModel->setAttribute('toDate', null);
+ $expected = <<
+
+
HTML;
- $this->formModel->setAttribute('string', '2021-09-18');
$this->assertEqualsWithoutLE(
$expected,
Field::widget()->config($this->formModel, 'toDate')->date()->render(),
@@ -51,7 +64,7 @@ public function testValue(): void
public function testValueException(): void
{
$this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Date widget requires a string value.');
+ $this->expectExceptionMessage('Date widget requires a string or null value.');
Field::widget()->config($this->formModel, 'array')->date()->render();
}