mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Reinstated separated DateField functionality
This commit is contained in:
parent
a9f2e9e73d
commit
80723c077b
@ -9,6 +9,12 @@ use SilverStripe\i18n\i18n;
|
||||
*/
|
||||
class SeparatedDateField extends DateField
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $separator = '/';
|
||||
|
||||
public function Field($properties = array())
|
||||
{
|
||||
// Three separate fields for day, month and year
|
||||
@ -33,12 +39,47 @@ class SeparatedDateField extends DateField
|
||||
$fieldYear->setAttribute('placeholder', _t(__CLASS__ . '.YEAR', 'Year'));
|
||||
}
|
||||
|
||||
$format = $this->getDateFormat();
|
||||
$validFormat = (
|
||||
stripos($format, 'd') !== false
|
||||
&& stripos($format, 'm') !== false
|
||||
&& stripos($format, 'y') !== false
|
||||
);
|
||||
if (!$validFormat) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Invalid date format for field ordering: ' . $format
|
||||
. '. Requires "d", "m", and "y" values to determine order'
|
||||
);
|
||||
}
|
||||
|
||||
$fields = array();
|
||||
$fields[stripos($format, 'd')] = $fieldDay->Field();
|
||||
$fields[stripos($format, 'm')] = $fieldMonth->Field();
|
||||
$fields[stripos($format, 'y')] = $fieldYear->Field();
|
||||
ksort($fields);
|
||||
|
||||
|
||||
// Join all fields
|
||||
// @todo custom ordering based on locale
|
||||
$sep = ' <span class="separator">/</span> ';
|
||||
return $fieldDay->Field() . $sep
|
||||
. $fieldMonth->Field() . $sep
|
||||
. $fieldYear->Field();
|
||||
$sep = ' <span class="separator">' . $this->getSeparator() . '</span> ';
|
||||
return implode($sep, $fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $string
|
||||
* @return self
|
||||
*/
|
||||
public function setSeparator($separator)
|
||||
{
|
||||
$this->separator = $separator;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSeparator()
|
||||
{
|
||||
return $this->separator;
|
||||
}
|
||||
|
||||
/**
|
||||
|
53
tests/php/Forms/SeparatedDateFieldTest.php
Normal file
53
tests/php/Forms/SeparatedDateFieldTest.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\Forms\Tests;
|
||||
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Forms\DateField;
|
||||
use SilverStripe\Forms\SeparatedDateField;
|
||||
use SilverStripe\Forms\RequiredFields;
|
||||
use SilverStripe\i18n\i18n;
|
||||
use SilverStripe\ORM\FieldType\DBDatetime;
|
||||
|
||||
class SeparatedDateFieldTest extends SapphireTest
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
i18n::set_locale('en_NZ');
|
||||
DBDatetime::set_mock_now('2011-02-01 8:34:00');
|
||||
}
|
||||
|
||||
public function testFieldOrderingBasedOnLocale()
|
||||
{
|
||||
$dateField = new SeparatedDateField('Date');
|
||||
$dateField->setLocale('en_NZ');
|
||||
$this->assertRegExp('/.*[day].*[month].*[year]/', $dateField->Field());
|
||||
}
|
||||
|
||||
public function testFieldOrderingBasedOnDateFormat()
|
||||
{
|
||||
$dateField = new SeparatedDateField('Date');
|
||||
$dateField->setDateFormat('y/MM/dd');
|
||||
$this->assertRegExp('/.*[year].*[month].*[day]/', $dateField->Field());
|
||||
}
|
||||
|
||||
public function testCustomSeparator()
|
||||
{
|
||||
$dateField = new SeparatedDateField('Date');
|
||||
$dateField->setDateFormat('dd/MM/y');
|
||||
$dateField->setSeparator('###');
|
||||
$this->assertRegExp('/.*[day].*###.*[month].*###.*[day]/', $dateField->Field());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid date format
|
||||
*/
|
||||
public function testInvalidDateFormat()
|
||||
{
|
||||
$dateField = new SeparatedDateField('Date');
|
||||
$dateField->setDateFormat('y/MM');
|
||||
$dateField->Field();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user