Add a class to all action buttons (eg. Login Form and Password Form)

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

Silverstripe 4

How would I beable to add a class to all actions on the front end forms? I can add to forms I create myself just not the CMS ones.

I have tried to find code on google, but it seems outdated for SS4.

Thanks

Hey,
there are several ways.

I would use an extension for Form Fields.

//config.yml
SilverStripe\Forms\FormField:
  extensions:
    - Your\Namespace\MyFormFieldExtension

Then create the Extension
(insert namespace and usage imports as well)

class MyFormFieldExtension extends Extension {

    /**
     * @param FormField $context
     * @param array $properties
     */
    public function onBeforeRender($context, $properties) {

        if( $context instanceof FormAction ) {
            $context->addExtraClass('myExtraCssClass');
        }
    }
}

That’s all

yes, i am sure. This is from one of my projects, where i am using this approach. I guess there are missing imports in the extension class.

Ensure that you have all required imports:

<?php

namespace Your\Namespace;

use SilverStripe\Core\Extension;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\FormField;

class MyFormFieldExtension extends Extension {

    /**
     * @param FormField $context
     * @param array $properties
     */
    public function onBeforeRender($context, $properties) {

        if( $context instanceof FormAction ) {
            $context->addExtraClass('myExtraCssClass');
        }
    }
}
1 Like

Hi,

Unfortunately that did not work. Are you sure this is for SilverStripe 4?

Thanks