Permalink
Browse files

ENHANCEMENT Support for <legend> tags in CompositeField

  • Loading branch information...
1 parent b417daf commit 9190bc4893ce4378ee7d4b48976383462af3a4fe @chillu chillu committed Feb 16, 2012
Showing with 78 additions and 7 deletions.
  1. +57 −7 forms/CompositeField.php
  2. +21 −0 tests/forms/CompositeFieldTest.php
View
@@ -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 <legend> tag, otherwise its a 'title' attribute.
+ */
+ protected $legend;
+
public function __construct($children = null) {
if($children instanceof FieldList) {
$this->children = $children;
@@ -70,10 +77,6 @@ public function setID($id) {
return $this;
}
- public function Field() {
- return $this->FieldHolder();
- }
-
/**
* Accessor method for $this->children
* @return FieldList
@@ -101,6 +104,17 @@ public function getTag() {
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 @@ function extraClasses() {
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 .= '<legend>' . $this->legend . '<legend>';
+ }
+
+ $fs = $this->FieldList();
+ foreach($fs as $subfield) {
+ if($this->columnCount) {
+ $className = "column{$this->columnCount}";
+ if(!next($fs)) $className .= " lastcolumn";
+ $content .= "\n<div class=\"{$className}\">\n" . $subfield->Field() . "\n</div>\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 .= '<legend>' . $this->legend . '<legend>';
+ }
+
$fs = $this->FieldList();
foreach($fs as $subfield) {
if($this->columnCount) {
@@ -136,14 +182,18 @@ function FieldHolder() {
/**
* 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 .= '<legend>' . $this->legend . '<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";
@@ -41,4 +41,25 @@ function testTag() {
$this->assertStringStartsWith('<fieldset', trim($composite->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]);
+ }
}

0 comments on commit 9190bc4

Please sign in to comment.