From 6d0002780dc89af91354979cba4d09d2cf8946da Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Wed, 14 Jan 2015 12:55:41 +0000 Subject: [PATCH 1/2] API Adding default_classes to Form --- forms/Form.php | 21 +++++++++++++++++++++ tests/forms/FormTest.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/forms/Form.php b/forms/Form.php index 918f16bc1..1b6964cc4 100644 --- a/forms/Form.php +++ b/forms/Form.php @@ -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()}, diff --git a/tests/forms/FormTest.php b/tests/forms/FormTest.php index f9d6566a8..d6f89a413 100644 --- a/tests/forms/FormTest.php +++ b/tests/forms/FormTest.php @@ -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(); From 5d4c2c4b34e1a0d544ce1b39a8d6fd74420025d4 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Wed, 14 Jan 2015 13:07:41 +0000 Subject: [PATCH 2/2] API Adding default_classes to FormField --- forms/FormField.php | 21 +++++++++++++++ tests/forms/FormFieldTest.php | 49 +++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/forms/FormField.php b/forms/FormField.php index 4456d956a..decd3af3f 100644 --- a/forms/FormField.php +++ b/forms/FormField.php @@ -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); + } + } } /** diff --git a/tests/forms/FormFieldTest.php b/tests/forms/FormFieldTest.php index 7441222a4..83bac93e2 100644 --- a/tests/forms/FormFieldTest.php +++ b/tests/forms/FormFieldTest.php @@ -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'); @@ -187,4 +232,4 @@ class FormFieldTest extends SapphireTest implements TestOnly { } } -} \ No newline at end of file +}