Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

Templating field groups


Reply


3 Posts   2086 Views

Avatar
mobius

Community Member, 54 Posts

8 July 2009 at 10:29am

I had a need want to have my field groups formatted in fieldsets with legends rather than divs with labels as I believe the former is more semantically useful (ie I know that a fieldset is for grouping similar fields, but I don't know what a div groups).

I would have hoped that I would be able to achieve this with a .ss file, but it seems that /sapphire/forms/FieldGroup.php returns HTML directly using the function FieldHolder().

I was able to adjust the HTML code there to get the output I desired, but why isn't it in a template?

Avatar
Willr

Forum Moderator, 5513 Posts

9 July 2009 at 7:15pm

It is not in a template as we found early on having each form field in its own template caused a large performance lost, so as a workaround for that its in the php. You could use your own custom subclass of the field to override the template it outputs

Avatar
beezer

Community Member, 16 Posts

17 June 2011 at 1:04pm

I needed to do this for a form which renders dynamic fieldsets from a db query and ended up extending FieldGroup, here is the code if anyone is interested. Very straight-forward and quite specific (needed a table layout) but you can do whatever you want:

<?php
class PublishFieldGroup extends FieldGroup {
function Field() {
$fs = $this->FieldSet();
$spaceZebra = isset($this->zebra) ? " $this->zebra" : '';
$idAtt = isset($this->id) ? " id=\"{$this->id}\"" : '';
$content = "<tr>";
foreach($fs as $subfield) {
$childZebra = (!isset($childZebra) || $childZebra == "odd") ? "even" : "odd";
if($subfield->hasMethod('setZebra')) $subfield->setZebra($childZebra);
$content .= "<td class=\"publishFieldgroupField\">" . $subfield->{$this->subfieldParam}() . "</td>";
}
$content .= "</tr>";
return $content;
}
}
?>