Faloude Faloude - 1 month ago 29
PHP Question

SilverStripe login form customisation

What's the easiest way to customise the default silverstripe login form? My final goal is to apply

->setExtraClass('myClass')
to the login action button.

Answer

In SilverStripe 3.4 we can extend MemberLoginForm, set the action in our constructor and then set SilverStripe to use the custom log in class as the default log in class.

First we create a CustomMemberLoginForm class:

mysite/code/CustomMemberLoginForm.php

class CustomMemberLoginForm extends MemberLoginForm {

    public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) {

        $actions = FieldList::create(
            FormAction::create('dologin', _t('Member.BUTTONLOGIN', 'Log in'))->addExtraClass('myClass'),
            LiteralField::create(
                'forgotPassword',
                '<p id="ForgotPassword"><a href="Security/lostpassword">'
                . _t('Member.BUTTONLOSTPASSWORD', "I've lost my password") . '</a></p>'
            )
        );

        parent::__construct($controller, $name, $fields, $actions);
    }
}

In our mysite/_config.php we add the following code to tell SilverStripe to use our custom log in class:

mysite/_config.php

Object::useCustomClass('MemberLoginForm', 'CustomMemberLoginForm');
Comments