API Adding default_classes to Form

This commit is contained in:
Daniel Hensby 2015-01-14 12:55:41 +00:00
parent 33b18ce998
commit 6d0002780d
2 changed files with 55 additions and 0 deletions

View File

@ -141,6 +141,12 @@ class Form extends RequestHandler {
*/
protected $extraClasses = array();
/**
* @config
* @var array $default_classes The default classes to apply to the Form
*/
private static $default_classes = array();
/**
* @var string
*/
@ -224,6 +230,8 @@ class Form extends RequestHandler {
}
$this->securityToken = ($securityEnabled) ? new SecurityToken() : new NullSecurityToken();
$this->setupDefaultClasses();
}
private static $url_handlers = array(
@ -263,6 +271,19 @@ class Form extends RequestHandler {
return $this;
}
/**
* 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);
}
}
}
/**
* Handle a form submission. GET and POST requests behave identically.
* Populates the form with {@link loadDataFrom()}, calls {@link validate()},

View File

@ -478,6 +478,40 @@ class FormTest extends FunctionalTest {
);
}
public function testDefaultClasses() {
Config::nest();
Config::inst()->update('Form', 'default_classes', array(
'class1',
));
$form = $this->getStubForm();
$this->assertContains('class1', $form->extraClass(), 'Class list does not contain expected class');
Config::inst()->update('Form', 'default_classes', array(
'class1',
'class2',
));
$form = $this->getStubForm();
$this->assertContains('class1 class2', $form->extraClass(), 'Class list does not contain expected class');
Config::inst()->update('Form', 'default_classes', array(
'class3',
));
$form = $this->getStubForm();
$this->assertContains('class3', $form->extraClass(), 'Class list does not contain expected class');
$form->removeExtraClass('class3');
$this->assertNotContains('class3', $form->extraClass(), 'Class list contains unexpected class');
Config::unnest();
}
public function testAttributes() {
$form = $this->getStubForm();