Compare commits

...

7 Commits

Author SHA1 Message Date
Thomas Portelange a6eae6fd9b
Merge f38931f0b3 into 0c8fcfb54c 2024-05-08 05:30:36 +12:00
Guy Sartorelli 0c8fcfb54c
Merge pull request #11213 from creative-commoners/pulls/5/required-has-one
FIX Handle getting HasOneRelationFieldInterface passed as an array
2024-05-07 13:01:59 +12:00
Guy Sartorelli 3449d8bf21
FIX Revert "ENH Add Nice to DBField" (#11222)
This reverts commit b167f470d7.
2024-05-07 11:45:48 +12:00
Steve Boyd b8f0b8ca4f FIX Handle getting HasOneRelationFieldInterface passed as an array 2024-05-02 12:06:43 +12:00
Thomas Portelange f38931f0b3
add expanded values to dates 2024-04-08 17:11:46 +02:00
Thomas Portelange e3c075b7a8
add expanded values to DatetimeField 2024-04-08 17:10:43 +02:00
Thomas Portelange c0fc61be1e
add support for expanded values on TimeField 2024-04-08 17:08:00 +02:00
7 changed files with 68 additions and 19 deletions

View File

@ -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;
}

View File

@ -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()) {

View File

@ -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;

View File

@ -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;

View File

@ -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()) {

View File

@ -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 ''

View File

@ -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']));
}