Jump to:

23481 Posts in 18965 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » autocomplete="off"

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 885 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    autocomplete="off" Link to this post

    How can I add autocomplete="off" to a form field?

    $fields->push( new TextField( "Your Phone") );

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: autocomplete="off" Link to this post

    Afaik the only way to do that is extending the (Text)Field. Something like

    class MyAutocompleteTextField extends TextField {

       function Field() {
          $attributes = array(
             'type' => 'text',
             'class' => 'text' . ($this->extraClass() ? $this->extraClass() : ''),
             'id' => $this->id(),
             'name' => $this->Name(),
             'value' => $this->Value(),
             'tabindex' => $this->getTabIndex(),
             'maxlength' => ($this->maxLength) ? $this->maxLength : null,
             'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null
          );

          if($this->disabled) $attributes['disabled'] = 'disabled';

          // add the new attribute here: <==========
          $attributes['autocomplete'] = 'off';

          return $this->createTag('input', $attributes);
       }

    }

    Then

    $fields->push( new MyAutocompleteTextField( "Your Phone") );

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: autocomplete="off" Link to this post

    On a similar topic, what's the best way to add autocomplete = "off" to the password field on the Login page?

    Or all password fields for that matter.

    I don't want to hack the core.

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: autocomplete="off" Link to this post

    This is how I've done it, not the greatest but works...

    _config.php

    Object::useCustomClass('MemberLoginForm','CustomLoginForm');

    CustomLoginForm.php

    class CustomLoginForm extends MemberLoginForm {
       public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) {
          parent::__construct($controller, $name, $fields, $actions);
          Requirements::customScript(<<<JS
             jQuery(document).ready(function(){
                jQuery('#CustomLoginForm_LoginForm_Password').attr('autocomplete','off');
             });
    JS
    );
       }
    }

    885 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.