mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Add validation for GroupedDropdownField (closes #3923)
This commit is contained in:
parent
1b94aa2c04
commit
dfb9b76b95
@ -89,11 +89,42 @@ class GroupedDropdownField extends DropdownField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo Implement DropdownField::validate() with group validation support
|
* Validate this field
|
||||||
|
*
|
||||||
|
* @param Validator $validator
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validate($validator) {
|
public function validate($validator) {
|
||||||
|
$valid = false;
|
||||||
|
$source = $this->getSourceAsArray();
|
||||||
|
|
||||||
|
if ($this->value) {
|
||||||
|
foreach ($source as $value => $title) {
|
||||||
|
// Check if value matches an option, or is in a nested array of grouped options
|
||||||
|
if ((is_array($title) && array_key_exists($this->value, $title))
|
||||||
|
|| ($this->value == $value)
|
||||||
|
) {
|
||||||
|
$valid = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} elseif ($this->getHasEmptyDefault()) {
|
||||||
|
$valid = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$valid) {
|
||||||
|
$validator->validationError(
|
||||||
|
$this->name,
|
||||||
|
_t(
|
||||||
|
'DropdownField.SOURCE_VALIDATION',
|
||||||
|
"Please select a value within the list provided. {value} is not a valid option",
|
||||||
|
array('value' => $this->value)
|
||||||
|
),
|
||||||
|
"validation"
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
44
tests/forms/GroupedDropdownFieldTest.php
Normal file
44
tests/forms/GroupedDropdownFieldTest.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package framework
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
|
class GroupedDropdownFieldTest extends SapphireTest {
|
||||||
|
|
||||||
|
public function testValidation() {
|
||||||
|
$field = GroupedDropdownField::create('Test', 'Testing', array(
|
||||||
|
"1" => "One",
|
||||||
|
"Group One" => array(
|
||||||
|
"2" => "Two",
|
||||||
|
"3" => "Three"
|
||||||
|
),
|
||||||
|
"Group Two" => array(
|
||||||
|
"4" => "Four"
|
||||||
|
)
|
||||||
|
));
|
||||||
|
|
||||||
|
$validator = new RequiredFields();
|
||||||
|
|
||||||
|
$field->setValue("1");
|
||||||
|
$this->assertTrue($field->validate($validator));
|
||||||
|
|
||||||
|
//test grouped values
|
||||||
|
$field->setValue("3");
|
||||||
|
$this->assertTrue($field->validate($validator));
|
||||||
|
|
||||||
|
//non-existent value should make the field invalid
|
||||||
|
$field->setValue("Over 9000");
|
||||||
|
$this->assertFalse($field->validate($validator));
|
||||||
|
|
||||||
|
//empty string shouldn't validate
|
||||||
|
$field->setValue('');
|
||||||
|
$this->assertFalse($field->validate($validator));
|
||||||
|
|
||||||
|
//empty field should validate after being set
|
||||||
|
$field->setEmptyString('Empty String');
|
||||||
|
$field->setValue('');
|
||||||
|
$this->assertTrue($field->validate($validator));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user