diff --git a/src/Forms/DateField.php b/src/Forms/DateField.php
index 9122aa0b9..5b2ea0474 100644
--- a/src/Forms/DateField.php
+++ b/src/Forms/DateField.php
@@ -220,6 +220,8 @@ class DateField extends TextField
);
}
+
+
if ($this->getHTML5() && $this->locale) {
throw new \LogicException(
'Please opt-out of HTML5 processing of ISO 8601 dates via setHTML5(false) if using setLocale()'
@@ -353,9 +355,12 @@ class DateField extends TextField
public function performReadonlyTransformation()
{
- $field = $this->castedCopy(DateField_Disabled::class);
- $field->setValue($this->dataValue());
- $field->setReadonly(true);
+ $field = $this
+ ->castedCopy(DateField_Disabled::class)
+ ->setValue($this->dataValue())
+ ->setLocale($this->getLocale())
+ ->setReadonly(true);
+
return $field;
}
diff --git a/src/Forms/DateField_Disabled.php b/src/Forms/DateField_Disabled.php
index e0656f99a..22cd201c3 100644
--- a/src/Forms/DateField_Disabled.php
+++ b/src/Forms/DateField_Disabled.php
@@ -14,27 +14,51 @@ class DateField_Disabled extends DateField
protected $disabled = true;
- public function Field($properties = array())
+ public function Field($properties = [])
{
- if ($this->valueObj) {
- if ($this->valueObj->isToday()) {
- $val = Convert::raw2xml($this->valueObj->toString($this->getConfig('dateformat'))
- . ' (' . _t('SilverStripe\\Forms\\DateField.TODAY', 'today') . ')');
+ // Default display value
+ $displayValue = '(' . _t(DateField::class . '.NOTSET', 'not set') . ')';
+
+ $value = $this->dataValue();
+
+ if ($value) {
+ $value = $this->tidyInternal($value);
+ $df = new DBDate($this->name);
+ $df->setValue($value);
+
+ if ($df->IsToday()) {
+ // e.g. 2018-06-01 (today)
+ $format = '%s (%s)';
+ $infoComplement = _t(DateField::class . '.TODAY', 'today');
} else {
- $df = new DBDate($this->name);
- $df->setValue($this->dataValue());
- $val = Convert::raw2xml($this->valueObj->toString($this->getConfig('dateformat'))
- . ', ' . $df->Ago());
+ // e.g. 2018-06-01, 5 days ago
+ $format = '%s, %s';
+ $infoComplement = $df->Ago();
}
- } else {
- $val = '(' . _t('SilverStripe\\Forms\\DateField.NOTSET', 'not set') . ')';
+
+ // Render the display value with some complement of info
+ $displayValue = Convert::raw2xml(sprintf(
+ $format,
+ $this->Value(),
+ $infoComplement
+ ));
}
- return "ID() . "\">$val";
+ return sprintf(
+ "%s",
+ $this->ID(),
+ $displayValue
+ );
}
public function Type()
{
- return "date_disabled readonly";
+ return "date_disabled readonly " . parent::Type();
+ }
+
+ public function getHTML5()
+ {
+ // Always disable HTML5 feature when using the readonly field.
+ return false;
}
}
diff --git a/tests/php/Forms/DateField_DisabledTest.php b/tests/php/Forms/DateField_DisabledTest.php
new file mode 100644
index 000000000..a8e5e3d1f
--- /dev/null
+++ b/tests/php/Forms/DateField_DisabledTest.php
@@ -0,0 +1,79 @@
+setValue('2011-02-01')
+ ->Field();
+ $expected = '1/02/2011 (today)';
+ $this->assertEquals($expected, $actual);
+
+ // Test today's date with time
+ $actual = DateField_Disabled::create('Test')
+ ->setValue('2011-02-01 10:34:00')
+ ->Field();
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function testFieldWithDifferentDay()
+ {
+ // Test past
+ $actual = DateField_Disabled::create('Test')
+ ->setValue('2011-01-27')
+ ->Field();
+ $expected = '27/01/2011, 5 days ago';
+ $this->assertEquals($expected, $actual);
+
+ // Test future
+ $actual = DateField_Disabled::create('Test')
+ ->setValue('2011-02-06')
+ ->Field();
+ $expected = '6/02/2011, in 5 days';
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function testFieldWithDifferentLocal()
+ {
+ // Test different local
+ $actual = DateField_Disabled::create('Test')
+ ->setValue('2011-02-06')
+ ->setHTML5(false)
+ ->setLocale('de_DE')
+ ->Field();
+ $expected = '06.02.2011, in 5 days';
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function testFieldWithNonValue()
+ {
+ // Test none value
+ $actual = DateField_Disabled::create('Test')->Field();
+ $expected = '(not set)';
+ $this->assertEquals($expected, $actual);
+
+ $actual = DateField_Disabled::create('Test')->setValue('This is not a date')->Field();
+ $this->assertEquals($expected, $actual);
+ }
+}