Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » [SOLVED] Disable Browser's Auto-complete

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

Page: 1
Go to End
Author Topic: 1263 Views
  • NathanBrauer
    Avatar
    Community Member
    11 Posts

    [SOLVED] Disable Browser's Auto-complete Link to this post

    Hello!

    My apologies if this is asked elsewhere -- I tried searching the forums but only got threads relating to the old AutocompleteField.

    I want to append autocomplete="off" to one (or more) of my TextField()s but currently I have no idea how to do it and can't find any way to add arbituary attributes to any of the Form fields. Any tips?

    Thanks a million!
    Nathan J. Brauer

    P.S. I'm new to SS/Saphire but I'm becoming a fan very quickly. Hopefully the community is as great as the framework

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: [SOLVED] Disable Browser's Auto-complete Link to this post

    Sadly there is nothing in the FormField API for appending attributes. If you look at TextField::Field() you can see how tags are 'created' the form fields are currently still generated from the PHP for performance reasons.

    You could either add the autocomplete attribute via javascript which may work (haven't tested!) or create your own subclass of TextField and override the Field() method in your subclass. Then, in your forms you would use your subclass rather than TextField (but javascript would be easier)

  • NathanBrauer
    Avatar
    Community Member
    11 Posts

    Re: [SOLVED] Disable Browser's Auto-complete Link to this post

    Is there a way I can make the changes necessary to append attributes and submit them to be included in the official project?

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: [SOLVED] Disable Browser's Auto-complete Link to this post

    Sure! If you plan on contributing it back to core follow the instructions on http://doc.silverstripe.org/sapphire/en/misc/contributing. You will need to fork the sapphire git repo so you can submit the changes easily.

    In terms of what you need to change, I guess you need a couple functions for adding / removing / setting attributes and a map of attributes stored on each FormField (to replace the current hardcoded arrays)

    Unit tests for this to be included in core is a must

    Good luck!

  • NathanBrauer
    Avatar
    Community Member
    11 Posts

    Re: [SOLVED] Disable Browser's Auto-complete Link to this post

    Sounds good. I'll see if I can squeeze it into my weekend

  • NathanBrauer
    Avatar
    Community Member
    11 Posts

    Re: [SOLVED] Disable Browser's Auto-complete Link to this post

    FYI - to those searching via Google.

    You can now do this using the ->setAttribute method.

    $fields->add(
    TextField::create('AccessCode','Access Code')->setAttribute('autocomplete','off')
    );

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