Version 5 supported

Form templates

Most markup generated in Silverstripe CMS can be replaced by custom templates. Both Form and FormField instances can be rendered out using custom templates using setTemplate.

$form = Form::create(/* ... */);

// or, just a field
$field = TextField::create(/* ... */);

To override the template for CMS forms, the custom templates should be located in app/templates/. Front-end form templates can be located in app/templates/ or in the active theme's templates/ directory.

It's recommended to copy the contents of the template you're going to replace and use that as a start. For instance, if you want to create a MyCustomFormTemplate copy the contents of to a file and modify as you need.

The default can be found at /vendor/silverstripe/framework/templates/SilverStripe/Forms/Includes/

By default, Form and the various FormField subclasses follow the Silverstripe CMS Template convention and are rendered into templates of the same class name (i.e.EmailField will attempt to render into and if that isn't found, or finally

While you can override all templates using normal view inheritance (i.e.defining a other modules may rely on the core template structure. It is recommended to use setTemplate and unique templates for specific forms.

For FormField instances, there are several other templates that are used on top of the main setTemplate().

$field = TextField::create();

// Sets the template for the <input> tag. i.e.'<input $AttributesHTML />'

// Sets the template for the wrapper around the text field. i.e.
//    '<div class="text">'
// The actual FormField is rendered into the holder via the `$Field`
// variable.
// setFieldHolder() is used in most `Form` instances and needs to output
// labels, error messages and the like.

// Sets the template for the wrapper around the text field.
// The difference here is the small field holder template is used when the
// field is embedded within another field. For example, if the field is
// part of a `FieldGroup` or `CompositeField` alongside other fields.

All templates are rendered within the scope of the FormField. To understand more about Scope within Templates as well as the available syntax, see the Templates documentation.

Related documentation

API documentation