Merge pull request #2166 from dhensby/patch-2

FormFields now allow setting of extra CSSClasses en masse
This commit is contained in:
Ingo Schommer 2013-09-24 11:50:01 -07:00
commit 48021e9fd3
4 changed files with 120 additions and 16 deletions

View File

@ -1475,14 +1475,12 @@ class Form extends RequestHandler {
* names delimited by a single space. * names delimited by a single space.
*/ */
public function addExtraClass($class) { public function addExtraClass($class) {
$classes = explode(' ', $class); //split at white space
$classes = preg_split('/\s+/', $class);
foreach ($classes as $class) { foreach ($classes as $class) {
$value = trim($class); //add classes one by one
$this->extraClasses[$class] = $class;
$this->extraClasses[] = $value;
} }
return $this; return $this;
} }
@ -1493,8 +1491,12 @@ class Form extends RequestHandler {
* @param string $class * @param string $class
*/ */
public function removeExtraClass($class) { public function removeExtraClass($class) {
$classes = explode(' ', $class); //split at white space
$this->extraClasses = array_diff($this->extraClasses, $classes); $classes = preg_split('/\s+/', $class);
foreach ($classes as $class) {
//unset one by one
unset($this->extraClasses[$class]);
}
return $this; return $this;
} }

View File

@ -293,24 +293,32 @@ class FormField extends RequestHandler {
} }
/** /**
* Add a CSS-class to the formfield-container. * Add one or more CSS-classes to the formfield-container.
* *
* @param $class String * @param $class String
*/ */
public function addExtraClass($class) { public function addExtraClass($class) {
//split at white space to extract all the classes
$classes = preg_split('/\s+/', $class);
foreach ($classes as $class) {
//add each class one by one
$this->extraClasses[$class] = $class; $this->extraClasses[$class] = $class;
}
return $this; return $this;
} }
/** /**
* Remove a CSS-class from the formfield-container. * Remove one or more CSS-classes from the formfield-container.
* *
* @param $class String * @param $class String
*/ */
public function removeExtraClass($class) { public function removeExtraClass($class) {
$pos = array_search($class, $this->extraClasses); //split at white space to extract all the classes
if($pos !== false) unset($this->extraClasses[$pos]); $classes = preg_split('/\s+/', $class);
foreach ($classes as $class) {
//unset each class one by one
unset($this->extraClasses[$class]);
}
return $this; return $this;
} }

View File

@ -21,6 +21,45 @@ class FormFieldTest extends SapphireTest {
$this->assertStringEndsWith('class2', $field->extraClass()); $this->assertStringEndsWith('class2', $field->extraClass());
} }
public function testAddManyExtraClasses() {
$field = new FormField('MyField');
//test we can split by a range of spaces and tabs
$field->addExtraClass('class1 class2 class3 class4 class5');
$this->assertStringEndsWith(
'class1 class2 class3 class4 class5',
$field->extraClass()
);
//test that duplicate classes don't get added
$field->addExtraClass('class1 class2');
$this->assertStringEndsWith(
'class1 class2 class3 class4 class5',
$field->extraClass()
);
}
public function testRemoveManyExtraClasses() {
$field = new FormField('MyField');
$field->addExtraClass('class1 class2 class3 class4 class5');
//test we can remove a single class we just added
$field->removeExtraClass('class3');
$this->assertStringEndsWith(
'class1 class2 class4 class5',
$field->extraClass()
);
//check we can remove many classes at once
$field->removeExtraClass('class1 class5');
$this->assertStringEndsWith(
'class2 class4',
$field->extraClass()
);
//check that removing a dud class is fine
$field->removeExtraClass('dudClass');
$this->assertStringEndsWith(
'class2 class4',
$field->extraClass()
);
}
public function testAttributes() { public function testAttributes() {
$field = new FormField('MyField'); $field = new FormField('MyField');
$field->setAttribute('foo', 'bar'); $field->setAttribute('foo', 'bar');

View File

@ -384,6 +384,61 @@ class FormTest extends FunctionalTest {
$this->assertEquals('application/x-www-form-urlencoded', $form->getEncType()); $this->assertEquals('application/x-www-form-urlencoded', $form->getEncType());
} }
public function testAddExtraClass() {
$form = $this->getStubForm();
$form->addExtraClass('class1');
$form->addExtraClass('class2');
$this->assertStringEndsWith('class1 class2', $form->extraClass());
}
public function testRemoveExtraClass() {
$form = $this->getStubForm();
$form->addExtraClass('class1');
$form->addExtraClass('class2');
$this->assertStringEndsWith('class1 class2', $form->extraClass());
$form->removeExtraClass('class1');
$this->assertStringEndsWith('class2', $form->extraClass());
}
public function testAddManyExtraClasses() {
$form = $this->getStubForm();
//test we can split by a range of spaces and tabs
$form->addExtraClass('class1 class2 class3 class4 class5');
$this->assertStringEndsWith(
'class1 class2 class3 class4 class5',
$form->extraClass()
);
//test that duplicate classes don't get added
$form->addExtraClass('class1 class2');
$this->assertStringEndsWith(
'class1 class2 class3 class4 class5',
$form->extraClass()
);
}
public function testRemoveManyExtraClasses() {
$form = $this->getStubForm();
$form->addExtraClass('class1 class2 class3 class4 class5');
//test we can remove a single class we just added
$form->removeExtraClass('class3');
$this->assertStringEndsWith(
'class1 class2 class4 class5',
$form->extraClass()
);
//check we can remove many classes at once
$form->removeExtraClass('class1 class5');
$this->assertStringEndsWith(
'class2 class4',
$form->extraClass()
);
//check that removing a dud class is fine
$form->removeExtraClass('dudClass');
$this->assertStringEndsWith(
'class2 class4',
$form->extraClass()
);
}
public function testAttributes() { public function testAttributes() {
$form = $this->getStubForm(); $form = $this->getStubForm();