BUGFIX: Fixed issue where TimeField_Readonly would only show "(not set)" instead of the value

This commit is contained in:
UndefinedOffset 2020-02-24 09:59:00 -04:00
parent 91f091f418
commit bba0f2f72f
No known key found for this signature in database
GPG Key ID: 59C4EE2B6468B796
3 changed files with 58 additions and 11 deletions

View File

@ -373,6 +373,14 @@ class TimeField extends TextField
{
/** @var TimeField_Readonly $result */
$result = $this->castedCopy(TimeField_Readonly::class);
$result
->setValue(false)
->setHTML5($this->html5)
->setTimeFormat($this->timeFormat)
->setTimezone($this->getTimezone())
->setLocale($this->locale)
->setTimeLength($this->timeLength)
->setValue($this->value);
return $result;
}

View File

@ -9,18 +9,11 @@ use SilverStripe\Core\Convert;
*/
class TimeField_Readonly extends TimeField
{
protected $readonly = true;
public function Field($properties = array())
protected $disabled = true;
public function Type()
{
if ($this->valueObj) {
$val = Convert::raw2xml($this->valueObj->toString($this->getConfig('timeformat')));
} else {
// TODO Localization
$val = '<i>(not set)</i>';
}
return "<span class=\"readonly\" id=\"" . $this->ID() . "\">$val</span>";
return 'readonly';
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace SilverStripe\Forms;
use IntlDateFormatter;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\TimeField;
use SilverStripe\Forms\TimeField_Readonly;
use SilverStripe\i18n\i18n;
class TimeFieldReadonlyTest extends SapphireTest
{
protected function setUp()
{
parent::setUp();
i18n::set_locale('en_NZ');
}
public function testPerformReadonly()
{
$field = new TimeField('Time', 'Time', '23:00:00');
$roField = $field->performReadonlyTransformation();
$this->assertInstanceOf(TimeField_Readonly::class, $roField);
$this->assertTrue($roField->isReadonly());
$this->assertEquals($roField->dataValue(), '23:00:00');
}
public function testSettingsCarryOver()
{
$field = new TimeField('Time', 'Time');
$field
->setHTML5(false)
->setTimeFormat('KK:mma')
->setTimezone('America/Halifax')
->setLocale('en_US')
->setTimeLength(IntlDateFormatter::SHORT)
->setValue('23:00:00');
$roField = $field->performReadonlyTransformation();
$this->assertFalse($roField->getHTML5());
$this->assertEquals($roField->getTimeFormat(), 'KK:mma');
$this->assertEquals($roField->getTimezone(), 'America/Halifax');
$this->assertEquals($roField->getLocale(), 'en_US');
$this->assertEquals($roField->getTimeLength(), IntlDateFormatter::SHORT);
}
}