Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Templating field groups

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

Page: 1
Go to End
Author Topic: 2048 Views
  • mobius
    Avatar
    Community Member
    54 Posts

    Templating field groups Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Templating field groups Link to this post

    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

  • beezer
    Avatar
    Community Member
    16 Posts

    Re: Templating field groups Link to this post

    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;
    }
    }
    ?>

    2048 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.