Merge pull request #4848 from kinglozzer/optionsetfield-no-value

FIX: Allow omitting a value for OptionsetField submissions (fixes #4284)
This commit is contained in:
Damian Mooyman 2015-12-15 15:12:07 +13:00
commit 37874936b5
2 changed files with 36 additions and 25 deletions

View File

@ -53,10 +53,8 @@
class OptionsetField extends DropdownField {
/**
* @var Array
* {@inheritdoc}
*/
protected $disabledItems = array();
public function Field($properties = array()) {
$source = $this->getSource();
$odd = 0;
@ -90,31 +88,15 @@ class OptionsetField extends DropdownField {
);
}
public function performReadonlyTransformation() {
// Source and values are DataObject sets.
$field = $this->castedCopy('LookupField');
$field->setSource($this->getSource());
$field->setReadonly(true);
return $field;
}
/**
* Mark certain elements as disabled,
* regardless of the {@link setDisabled()} settings.
*
* @param array $items Collection of array keys, as defined in the $source array
* {@inheritdoc}
*/
public function setDisabledItems($items) {
$this->disabledItems = $items;
return $this;
}
public function validate($validator) {
if (!$this->value) {
return true;
}
/**
* @return Array
*/
public function getDisabledItems() {
return $this->disabledItems;
return parent::validate($validator);
}
public function ExtraOptions() {

View File

@ -25,6 +25,35 @@ class OptionsetFieldTest extends SapphireTest {
);
}
public function testValidation() {
$field = OptionsetField::create('Test', 'Testing', array(
"One" => "One",
"Two" => "Two",
"Five" => "Five"
));
$validator = new RequiredFields('Test');
$form = new Form($this, 'Form', new FieldList($field), new FieldList(), $validator);
$field->setValue("One");
$this->assertTrue($field->validate($validator));
//non-existent value should make the field invalid
$field->setValue("Three");
$this->assertFalse($field->validate($validator));
//empty string should pass field-level validation...
$field->setValue('');
$this->assertTrue($field->validate($validator));
// ... but should not pass "RequiredFields" validation
$this->assertFalse($form->validate());
//disabled items shouldn't validate
$field->setDisabledItems(array('Five'));
$field->setValue('Five');
$this->assertFalse($field->validate($validator));
}
public function testReadonlyField() {
$sourceArray = array(0 => 'No', 1 => 'Yes');
$field = new OptionsetField('FeelingOk', 'are you feeling ok?', $sourceArray, 1);