From 9190bc4893ce4378ee7d4b48976383462af3a4fe Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 16 Feb 2012 16:40:19 +0100 Subject: [PATCH] ENHANCEMENT Support for tags in CompositeField --- forms/CompositeField.php | 64 ++++++++++++++++++++++++++---- tests/forms/CompositeFieldTest.php | 21 ++++++++++ 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/forms/CompositeField.php b/forms/CompositeField.php index 3ff72f734..13377a14f 100644 --- a/forms/CompositeField.php +++ b/forms/CompositeField.php @@ -33,6 +33,13 @@ class CompositeField extends FormField { */ protected $tag = 'div'; + /** + * @var String Optional description for this set of fields. + * If the {@link $tag} property is set to use a 'fieldset', this will be + * rendered as a tag, otherwise its a 'title' attribute. + */ + protected $legend; + public function __construct($children = null) { if($children instanceof FieldList) { $this->children = $children; @@ -70,10 +77,6 @@ class CompositeField extends FormField { return $this; } - public function Field() { - return $this->FieldHolder(); - } - /** * Accessor method for $this->children * @return FieldList @@ -101,6 +104,17 @@ class CompositeField extends FormField { return $this->tag; } + /** @param String */ + public function setLegend($legend) { + $this->legend = $legend; + return $this; + } + + /** @return String */ + public function getLegend() { + return $this->legend; + } + function extraClasses() { $classes = array('field', 'CompositeField', parent::extraClasses()); if($this->columnCount) $classes[] = 'multicolumn'; @@ -110,15 +124,47 @@ class CompositeField extends FormField { function getAttributes() { return array_merge( parent::getAttributes(), - array('tabindex' => null, 'type' => null, 'value' => null, 'type' => null) + array( + 'tabindex' => null, + 'type' => null, + 'value' => null, + 'type' => null, + 'title' => ($this->tag == 'fieldset') ? null : $this->legend + ) ); } + public function Field() { + $content = ''; + + if($this->tag == 'fieldset' && $this->legend) { + $content .= '' . $this->legend . ''; + } + + $fs = $this->FieldList(); + foreach($fs as $subfield) { + if($this->columnCount) { + $className = "column{$this->columnCount}"; + if(!next($fs)) $className .= " lastcolumn"; + $content .= "\n
\n" . $subfield->Field() . "\n
\n"; + } else if($subfield){ + $content .= "\n" . $subfield->Field() . "\n"; + } + } + + return $this->createTag($this->getTag(), $this->getAttributes(), $content); + } + /** * Returns the fields nested inside another DIV */ function FieldHolder() { $content = ''; + + if($this->tag == 'fieldset' && $this->legend) { + $content .= '' . $this->legend . ''; + } + $fs = $this->FieldList(); foreach($fs as $subfield) { if($this->columnCount) { @@ -136,14 +182,18 @@ class CompositeField extends FormField { /** * Returns the fields in the restricted field holder inside a DIV. */ - function SmallFieldHolder() {//return $this->FieldHolder(); + function SmallFieldHolder() { $fs = $this->FieldList(); $tag = $this->getTag(); $idAtt = isset($this->id) ? " id=\"{$this->id}\"" : ''; $className = ($this->columnCount) ? "field CompositeField {$this->extraClass()} multicolumn" : "field CompositeField {$this->extraClass()}"; $content = "<$tag class=\"$className\"$idAtt>"; + + if($this->tag == 'fieldset' && $this->legend) { + $content .= '' . $this->legend . ''; + } - foreach($fs as $subfield) {//echo ' subf'.$subfield->getName(); + foreach($fs as $subfield) { if($this->columnCount) { $className = "column{$this->columnCount}"; if(!next($fs)) $className .= " lastcolumn"; diff --git a/tests/forms/CompositeFieldTest.php b/tests/forms/CompositeFieldTest.php index f2c8d596c..8c4f3e938 100644 --- a/tests/forms/CompositeFieldTest.php +++ b/tests/forms/CompositeFieldTest.php @@ -41,4 +41,25 @@ class CompositeFieldTest extends SapphireTest { $this->assertStringStartsWith('FieldHolder())); $this->assertStringEndsWith('/fieldset>', trim($composite->FieldHolder())); } + + function testLegend() { + $composite = new CompositeField( + new TextField('A'), + new TextField('B') + ); + $composite->setLegend('My legend'); + $parser = new CSSContentParser($composite->Field()); + $root = $parser->getBySelector('div.composite'); + $this->assertObjectHasAttribute('title', $root[0]->attributes()); + $this->assertEquals('My legend', (string)$root[0]['title']); + + $composite->setTag('fieldset'); + $composite->setLegend('My legend'); + $parser = new CSSContentParser($composite->Field()); + $root = $parser->getBySelector('fieldset.composite'); + $this->assertObjectNotHasAttribute('title', $root[0]->attributes()); + $legend = $parser->getBySelector('fieldset.composite legend'); + $this->assertNotNull($legend); + $this->assertEquals('My legend', (string)$legend[0]); + } }