mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX Add extraEmptyValues to TreedropdownField
This commit is contained in:
parent
7eab49f850
commit
4dbbf04ba5
@ -116,8 +116,15 @@ class RequiredFields extends Validator
|
|||||||
$error = (count($value ?? [])) ? false : true;
|
$error = (count($value ?? [])) ? false : true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// assume a string or integer
|
$stringValue = (string) $value;
|
||||||
$error = (strlen($value ?? '')) ? false : true;
|
if ($formField instanceof TreeDropdownField) {
|
||||||
|
// test for blank string as well as '0' because older versions of silverstripe/admin FormBuilder
|
||||||
|
// forms created using redux-form would have a value of null for unsaved records
|
||||||
|
// the null value will have been converted to '' by the time it gets to this point
|
||||||
|
$error = in_array($stringValue, ['0', '']);
|
||||||
|
} else {
|
||||||
|
$error = strlen($stringValue) > 0 ? false : true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($formField && $error) {
|
if ($formField && $error) {
|
||||||
|
@ -249,7 +249,12 @@ class TreeDropdownField extends FormField
|
|||||||
|
|
||||||
$this->addExtraClass('single');
|
$this->addExtraClass('single');
|
||||||
|
|
||||||
parent::__construct($name, $title);
|
// Set a default value of 0 instead of null
|
||||||
|
// Because TreedropdownField requires SourceObject to have the Hierarchy extension, make the default
|
||||||
|
// value the same as the default value for a RelationID, which is 0.
|
||||||
|
$value = 0;
|
||||||
|
|
||||||
|
parent::__construct($name, $title, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -981,4 +986,16 @@ class TreeDropdownField extends FormField
|
|||||||
$this->showSelectedPath = $showSelectedPath;
|
$this->showSelectedPath = $showSelectedPath;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getSchemaValidation()
|
||||||
|
{
|
||||||
|
$validationList = parent::getSchemaValidation();
|
||||||
|
if (array_key_exists('required', $validationList)) {
|
||||||
|
$validationList['required'] = ['extraEmptyValues' => ['0']];
|
||||||
|
}
|
||||||
|
return $validationList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,9 @@ namespace SilverStripe\Forms\Tests;
|
|||||||
|
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
use SilverStripe\Forms\RequiredFields;
|
use SilverStripe\Forms\RequiredFields;
|
||||||
|
use SilverStripe\Forms\Form;
|
||||||
|
use SilverStripe\Forms\TreeDropdownField;
|
||||||
|
use SilverStripe\Security\Group;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo Test the validation method php()
|
* @todo Test the validation method php()
|
||||||
@ -11,7 +14,6 @@ use SilverStripe\Forms\RequiredFields;
|
|||||||
*/
|
*/
|
||||||
class RequiredFieldsTest extends SapphireTest
|
class RequiredFieldsTest extends SapphireTest
|
||||||
{
|
{
|
||||||
|
|
||||||
public function testConstructingWithArray()
|
public function testConstructingWithArray()
|
||||||
{
|
{
|
||||||
//can we construct with an array?
|
//can we construct with an array?
|
||||||
@ -290,4 +292,18 @@ class RequiredFieldsTest extends SapphireTest
|
|||||||
"Unexpectedly returned true for a non-existent field"
|
"Unexpectedly returned true for a non-existent field"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testTreedropFieldValidation()
|
||||||
|
{
|
||||||
|
$form = new Form();
|
||||||
|
$field = new TreeDropdownField('TestField', 'TestField', Group::class);
|
||||||
|
$form->Fields()->push($field);
|
||||||
|
$validator = new RequiredFields('TestField');
|
||||||
|
$validator->setForm($form);
|
||||||
|
// blank string and '0' are fail required field validation
|
||||||
|
$this->assertFalse($validator->php(['TestField' => '']));
|
||||||
|
$this->assertFalse($validator->php(['TestField' => '0']));
|
||||||
|
// '1' passes required field validation
|
||||||
|
$this->assertTrue($validator->php(['TestField' => '1']));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ use SilverStripe\Dev\SapphireTest;
|
|||||||
use SilverStripe\Control\HTTPRequest;
|
use SilverStripe\Control\HTTPRequest;
|
||||||
use SilverStripe\Forms\FieldList;
|
use SilverStripe\Forms\FieldList;
|
||||||
use SilverStripe\Forms\Form;
|
use SilverStripe\Forms\Form;
|
||||||
|
use SilverStripe\Forms\RequiredFields;
|
||||||
use SilverStripe\Forms\TreeDropdownField;
|
use SilverStripe\Forms\TreeDropdownField;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
use SilverStripe\ORM\Tests\HierarchyTest\HierarchyOnSubclassTestObject;
|
use SilverStripe\ORM\Tests\HierarchyTest\HierarchyOnSubclassTestObject;
|
||||||
@ -52,6 +53,22 @@ class TreeDropdownFieldTest extends SapphireTest
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetSchemaValidation(): void
|
||||||
|
{
|
||||||
|
// field is not required
|
||||||
|
$field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);
|
||||||
|
$expected = [];
|
||||||
|
$this->assertSame($expected, $field->getSchemaValidation());
|
||||||
|
// field is required
|
||||||
|
$fieldList = new FieldList([$field]);
|
||||||
|
$validator = new RequiredFields('TestTree');
|
||||||
|
new Form(null, null, $fieldList, null, $validator);
|
||||||
|
$expected = [
|
||||||
|
'required' => ['extraEmptyValues' => ['0']],
|
||||||
|
];
|
||||||
|
$this->assertSame($expected, $field->getSchemaValidation());
|
||||||
|
}
|
||||||
|
|
||||||
public function testTreeSearchJson()
|
public function testTreeSearchJson()
|
||||||
{
|
{
|
||||||
$field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);
|
$field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);
|
||||||
|
Loading…
Reference in New Issue
Block a user