API Adding default_classes to FormField

This commit is contained in:
Daniel Hensby 2015-01-14 13:07:41 +00:00
parent 6d0002780d
commit 5d4c2c4b34
2 changed files with 68 additions and 2 deletions

View File

@ -41,6 +41,12 @@ class FormField extends RequestHandler {
*/
protected $extraClasses;
/**
* @config
* @var array $default_classes The default classes to apply to the FormField
*/
private static $default_classes = array();
public $dontEscape;
/**
@ -164,6 +170,21 @@ class FormField extends RequestHandler {
if($value !== NULL) $this->setValue($value);
parent::__construct();
$this->setupDefaultClasses();
}
/**
* set up the default classes for the form. This is done on construct so that the default classes can be removed
* after instantiation
*/
protected function setupDefaultClasses() {
$defaultClasses = self::config()->get('default_classes');
if ($defaultClasses) {
foreach ($defaultClasses as $class) {
$this->addExtraClass($class);
}
}
}
/**

View File

@ -3,7 +3,52 @@
* @package framework
* @subpackage tests
*/
class FormFieldTest extends SapphireTest implements TestOnly {
class FormFieldTest extends SapphireTest {
public function testDefaultClasses() {
Config::nest();
Config::inst()->update('FormField', 'default_classes', array(
'class1',
));
$field = new FormField('MyField');
$this->assertContains('class1', $field->extraClass(), 'Class list does not contain expected class');
Config::inst()->update('FormField', 'default_classes', array(
'class1',
'class2',
));
$field = new FormField('MyField');
$this->assertContains('class1 class2', $field->extraClass(), 'Class list does not contain expected class');
Config::inst()->update('FormField', 'default_classes', array(
'class3',
));
$field = new FormField('MyField');
$this->assertContains('class3', $field->extraClass(), 'Class list does not contain expected class');
$field->removeExtraClass('class3');
$this->assertNotContains('class3', $field->extraClass(), 'Class list contains unexpected class');
Config::inst()->update('TextField', 'default_classes', array(
'textfield-class',
));
$field = new TextField('MyField');
//check default classes inherit
$this->assertContains('class3', $field->extraClass(), 'Class list does not contain inherited class');
$this->assertContains('textfield-class', $field->extraClass(), 'Class list does not contain expected class');
Config::unnest();
}
public function testAddExtraClass() {
$field = new FormField('MyField');