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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Templating field groups


Go to End


3 Posts   3152 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, 5523 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
ec8or

Community Member, 18 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;
    }
}
?>