mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API Adding default_classes to Form
This commit is contained in:
parent
33b18ce998
commit
6d0002780d
@ -141,6 +141,12 @@ class Form extends RequestHandler {
|
|||||||
*/
|
*/
|
||||||
protected $extraClasses = array();
|
protected $extraClasses = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @config
|
||||||
|
* @var array $default_classes The default classes to apply to the Form
|
||||||
|
*/
|
||||||
|
private static $default_classes = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
@ -224,6 +230,8 @@ class Form extends RequestHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->securityToken = ($securityEnabled) ? new SecurityToken() : new NullSecurityToken();
|
$this->securityToken = ($securityEnabled) ? new SecurityToken() : new NullSecurityToken();
|
||||||
|
|
||||||
|
$this->setupDefaultClasses();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static $url_handlers = array(
|
private static $url_handlers = array(
|
||||||
@ -263,6 +271,19 @@ class Form extends RequestHandler {
|
|||||||
return $this;
|
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.
|
* Handle a form submission. GET and POST requests behave identically.
|
||||||
* Populates the form with {@link loadDataFrom()}, calls {@link validate()},
|
* Populates the form with {@link loadDataFrom()}, calls {@link validate()},
|
||||||
|
@ -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() {
|
public function testAttributes() {
|
||||||
$form = $this->getStubForm();
|
$form = $this->getStubForm();
|
||||||
|
Loading…
Reference in New Issue
Block a user