ENHANCEMENT Support for <legend> tags in CompositeField

This commit is contained in:
Ingo Schommer 2012-02-16 16:40:19 +01:00
parent b417daf7af
commit 9190bc4893
2 changed files with 78 additions and 7 deletions

View File

@ -33,6 +33,13 @@ class CompositeField extends FormField {
*/ */
protected $tag = 'div'; 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) { public function __construct($children = null) {
if($children instanceof FieldList) { if($children instanceof FieldList) {
$this->children = $children; $this->children = $children;
@ -70,10 +77,6 @@ class CompositeField extends FormField {
return $this; return $this;
} }
public function Field() {
return $this->FieldHolder();
}
/** /**
* Accessor method for $this->children * Accessor method for $this->children
* @return FieldList * @return FieldList
@ -101,6 +104,17 @@ class CompositeField extends FormField {
return $this->tag; return $this->tag;
} }
/** @param String */
public function setLegend($legend) {
$this->legend = $legend;
return $this;
}
/** @return String */
public function getLegend() {
return $this->legend;
}
function extraClasses() { function extraClasses() {
$classes = array('field', 'CompositeField', parent::extraClasses()); $classes = array('field', 'CompositeField', parent::extraClasses());
if($this->columnCount) $classes[] = 'multicolumn'; if($this->columnCount) $classes[] = 'multicolumn';
@ -110,15 +124,47 @@ class CompositeField extends FormField {
function getAttributes() { function getAttributes() {
return array_merge( return array_merge(
parent::getAttributes(), 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 * Returns the fields nested inside another DIV
*/ */
function FieldHolder() { function FieldHolder() {
$content = ''; $content = '';
if($this->tag == 'fieldset' && $this->legend) {
$content .= '<legend>' . $this->legend . '<legend>';
}
$fs = $this->FieldList(); $fs = $this->FieldList();
foreach($fs as $subfield) { foreach($fs as $subfield) {
if($this->columnCount) { if($this->columnCount) {
@ -136,14 +182,18 @@ class CompositeField extends FormField {
/** /**
* Returns the fields in the restricted field holder inside a DIV. * Returns the fields in the restricted field holder inside a DIV.
*/ */
function SmallFieldHolder() {//return $this->FieldHolder(); function SmallFieldHolder() {
$fs = $this->FieldList(); $fs = $this->FieldList();
$tag = $this->getTag(); $tag = $this->getTag();
$idAtt = isset($this->id) ? " id=\"{$this->id}\"" : ''; $idAtt = isset($this->id) ? " id=\"{$this->id}\"" : '';
$className = ($this->columnCount) ? "field CompositeField {$this->extraClass()} multicolumn" : "field CompositeField {$this->extraClass()}"; $className = ($this->columnCount) ? "field CompositeField {$this->extraClass()} multicolumn" : "field CompositeField {$this->extraClass()}";
$content = "<$tag class=\"$className\"$idAtt>"; $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) { if($this->columnCount) {
$className = "column{$this->columnCount}"; $className = "column{$this->columnCount}";
if(!next($fs)) $className .= " lastcolumn"; if(!next($fs)) $className .= " lastcolumn";

View File

@ -41,4 +41,25 @@ class CompositeFieldTest extends SapphireTest {
$this->assertStringStartsWith('<fieldset', trim($composite->FieldHolder())); $this->assertStringStartsWith('<fieldset', trim($composite->FieldHolder()));
$this->assertStringEndsWith('/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]);
}
} }