Search code examples
phphtmlformssilverstripesilverstripe-4

Is there a way to add a wrapper div around a silverstripe form field


Is there a way to add a wrapper around a labelfield or any other kind of form field in silverstripe forms? It would be easier to style forms this way.

FieldList::create(
    LabelField::create('label','Label'),
    EmailField::create('Email',''),
    TextareaField::create('Comment','')
)

Solution

  • Yes, according to the docs you can set a template either per form or per field, see docs.

    $field = TextField::create(..);
    $field->setTemplate('MyCustomTextField');
    

    You need to set the whole path to your template if it's located in a subfolder of your themes /templates/ directory.

    If you need to change the templates globally you can overwrite them in your theme. LabelField's template is located in templates/SilverStripe/Forms/LabelField.ss.

    1. Put a file with the same path in your theme (e.g. themes/mytheme/templates/SilverStripe/Forms/LabelField.ss),

    2. flush your Silverstripe cache (e.g. by running flush by adding ?flush to the URL) so Silverstripe can find the new template file,

    3. and start experimenting with your custom markup.

    A flush is only needed when you add a new template file, updates of your existing files will be detected automatically.