FIX: Allow omitting a value for OptionsetField submissions (fixes #4824)

This commit is contained in:
Loz Calver 2015-12-14 16:41:18 +00:00
parent 9dabf15faa
commit d265c9b733
2 changed files with 40 additions and 0 deletions

View File

@ -117,6 +117,17 @@ class OptionsetField extends DropdownField {
return $this->disabledItems; return $this->disabledItems;
} }
/**
* {@inheritdoc}
*/
public function validate($validator) {
if (!$this->value) {
return true;
}
return parent::validate($validator);
}
public function ExtraOptions() { public function ExtraOptions() {
return new ArrayList(); return new ArrayList();
} }

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() { public function testReadonlyField() {
$sourceArray = array(0 => 'No', 1 => 'Yes'); $sourceArray = array(0 => 'No', 1 => 'Yes');
$field = new OptionsetField('FeelingOk', 'are you feeling ok?', $sourceArray, 1); $field = new OptionsetField('FeelingOk', 'are you feeling ok?', $sourceArray, 1);