Compare commits
7 Commits
5818f62f88
...
a6eae6fd9b
Author | SHA1 | Date |
---|---|---|
Thomas Portelange | a6eae6fd9b | |
Guy Sartorelli | 0c8fcfb54c | |
Guy Sartorelli | 3449d8bf21 | |
Steve Boyd | b8f0b8ca4f | |
Thomas Portelange | f38931f0b3 | |
Thomas Portelange | e3c075b7a8 | |
Thomas Portelange | c0fc61be1e |
|
@ -513,8 +513,8 @@ class DateField extends TextField
|
|||
* Convert frontend date to the internal representation (ISO 8601).
|
||||
* The frontend date is also in ISO 8601 when $html5=true.
|
||||
*
|
||||
* @param string $date
|
||||
* @return string The formatted date, or null if not a valid date
|
||||
* @param ?string $date
|
||||
* @return ?string The formatted date, or null if not a valid date
|
||||
*/
|
||||
protected function frontendToInternal($date)
|
||||
{
|
||||
|
@ -524,6 +524,17 @@ class DateField extends TextField
|
|||
$fromFormatter = $this->getFrontendFormatter();
|
||||
$toFormatter = $this->getInternalFormatter();
|
||||
$timestamp = $fromFormatter->parse($date);
|
||||
|
||||
// Retry with expanded value
|
||||
if ($timestamp === false) {
|
||||
$zeroFormat = $fromFormatter->format(strtotime(date('Y-01-01 00:00:00')));
|
||||
$expectedLength = strlen($zeroFormat);
|
||||
if (strlen($date) < $expectedLength) {
|
||||
$expandedValue = $date . substr($zeroFormat, strlen($date));
|
||||
$timestamp = $fromFormatter->parse($expandedValue);
|
||||
}
|
||||
}
|
||||
|
||||
if ($timestamp === false) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -179,8 +179,8 @@ class DatetimeField extends TextField
|
|||
* Assumes the value is in the defined {@link $timezone} (if one is set),
|
||||
* and adjusts for server timezone.
|
||||
*
|
||||
* @param string $datetime
|
||||
* @return string The formatted date, or null if not a valid date
|
||||
* @param ?string $datetime
|
||||
* @return ?string The formatted date, or null if not a valid date
|
||||
*/
|
||||
public function frontendToInternal($datetime)
|
||||
{
|
||||
|
@ -193,6 +193,16 @@ class DatetimeField extends TextField
|
|||
// Try to parse time with seconds
|
||||
$timestamp = $fromFormatter->parse($datetime);
|
||||
|
||||
// Retry with expanded value
|
||||
if ($timestamp === false) {
|
||||
$zeroFormat = $fromFormatter->format(strtotime(date('Y-01-01 00:00:00')));
|
||||
$expectedLength = strlen($zeroFormat);
|
||||
if (strlen($datetime) < $expectedLength) {
|
||||
$expandedValue = $datetime . substr($zeroFormat, strlen($datetime));
|
||||
$timestamp = $fromFormatter->parse($expandedValue);
|
||||
}
|
||||
}
|
||||
|
||||
// Try to parse time without seconds, since that's a valid HTML5 submission format
|
||||
// See https://html.spec.whatwg.org/multipage/infrastructure.html#times
|
||||
if ($timestamp === false && $this->getHTML5()) {
|
||||
|
|
|
@ -113,7 +113,12 @@ class RequiredFields extends Validator
|
|||
if ($formField instanceof FileField && isset($value['error']) && $value['error']) {
|
||||
$error = true;
|
||||
} else {
|
||||
$error = (count($value ?? [])) ? false : true;
|
||||
if (is_a($formField, HasOneRelationFieldInterface::class) && isset($value['value'])) {
|
||||
$stringValue = (string) $value['value'];
|
||||
$error = in_array($stringValue, ['0', '']);
|
||||
} else {
|
||||
$error = (count($value ?? [])) ? false : true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$stringValue = (string) $value;
|
||||
|
|
|
@ -5,8 +5,9 @@ namespace SilverStripe\Forms;
|
|||
use SilverStripe\Dev\Deprecation;
|
||||
use SilverStripe\Forms\DropdownField;
|
||||
use SilverStripe\ORM\DataList;
|
||||
use SilverStripe\Forms\HasOneRelationFieldInterface;
|
||||
|
||||
class SearchableDropdownField extends DropdownField
|
||||
class SearchableDropdownField extends DropdownField implements HasOneRelationFieldInterface
|
||||
{
|
||||
use SearchableDropdownTrait;
|
||||
|
||||
|
|
|
@ -388,8 +388,8 @@ class TimeField extends TextField
|
|||
* Convert frontend time to the internal representation (ISO 8601).
|
||||
* The frontend time is also in ISO 8601 when $html5=true.
|
||||
*
|
||||
* @param string $time
|
||||
* @return string The formatted time, or null if not a valid time
|
||||
* @param ?string $time
|
||||
* @return ?string The formatted time, or null if not a valid time
|
||||
*/
|
||||
protected function frontendToInternal($time)
|
||||
{
|
||||
|
@ -400,6 +400,16 @@ class TimeField extends TextField
|
|||
$toFormatter = $this->getInternalFormatter();
|
||||
$timestamp = $fromFormatter->parse($time);
|
||||
|
||||
// Retry with expanded value
|
||||
if ($timestamp === false) {
|
||||
$zeroFormat = $fromFormatter->format(strtotime(date('Y-01-01 00:00:00')));
|
||||
$expectedLength = strlen($zeroFormat);
|
||||
if (strlen($time) < $expectedLength) {
|
||||
$expandedValue = $time . substr($zeroFormat, strlen($time));
|
||||
$timestamp = $fromFormatter->parse($expandedValue);
|
||||
}
|
||||
}
|
||||
|
||||
// Try to parse time without seconds, since that's a valid HTML5 submission format
|
||||
// See https://html.spec.whatwg.org/multipage/infrastructure.html#times
|
||||
if ($timestamp === false && $this->getHTML5()) {
|
||||
|
|
|
@ -516,14 +516,6 @@ abstract class DBField extends ViewableData implements DBIndexable
|
|||
return $this->XML();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function Nice()
|
||||
{
|
||||
return $this->XML();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to be set in the database to blank this field.
|
||||
* Usually it's a choice between null, 0, and ''
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace SilverStripe\Forms\Tests;
|
|||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Forms\RequiredFields;
|
||||
use SilverStripe\Forms\Form;
|
||||
use SilverStripe\Forms\SearchableDropdownField;
|
||||
use SilverStripe\Forms\TreeDropdownField;
|
||||
use SilverStripe\Security\Group;
|
||||
|
||||
|
@ -289,16 +290,35 @@ class RequiredFieldsTest extends SapphireTest
|
|||
);
|
||||
}
|
||||
|
||||
public function testTreedropFieldValidation()
|
||||
public function provideHasOneRelationFieldInterfaceValidation(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'className' => TreeDropdownField::class,
|
||||
],
|
||||
[
|
||||
'className' => SearchableDropdownField::class,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideHasOneRelationFieldInterfaceValidation
|
||||
*/
|
||||
public function testHasOneRelationFieldInterfaceValidation(string $className)
|
||||
{
|
||||
$form = new Form();
|
||||
$field = new TreeDropdownField('TestField', 'TestField', Group::class);
|
||||
$param = $className === TreeDropdownField::class ? Group::class : Group::get();
|
||||
$field = new $className('TestField', 'TestField', $param);
|
||||
$form->Fields()->push($field);
|
||||
$validator = new RequiredFields('TestField');
|
||||
$validator->setForm($form);
|
||||
// blank string and '0' are fail required field validation
|
||||
// blank string and 0 and '0' and array with value of 0 fail required field validation
|
||||
$this->assertFalse($validator->php(['TestField' => '']));
|
||||
$this->assertFalse($validator->php(['TestField' => 0]));
|
||||
$this->assertFalse($validator->php(['TestField' => '0']));
|
||||
$this->assertFalse($validator->php(['TestField' => ['value' => 0]]));
|
||||
$this->assertFalse($validator->php(['TestField' => ['value' => '0']]));
|
||||
// '1' passes required field validation
|
||||
$this->assertTrue($validator->php(['TestField' => '1']));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue