Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

[SOLVED] Disable Browser's Auto-complete


Reply


6 Posts   1387 Views

Avatar
NathanBrauer

Community Member, 11 Posts

11 May 2011 at 12:54pm

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 :)

Avatar
Willr

Forum Moderator, 5513 Posts

12 May 2011 at 2:04pm

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)

Avatar
NathanBrauer

Community Member, 11 Posts

13 May 2011 at 2:03am

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

Avatar
Willr

Forum Moderator, 5513 Posts

13 May 2011 at 1:52pm

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!

Avatar
NathanBrauer

Community Member, 11 Posts

14 May 2011 at 5:04am

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

Avatar
NathanBrauer

Community Member, 11 Posts

20 April 2014 at 3:22am

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')
);