Created
October 15, 2012 15:17
-
-
Save saevarom/3893044 to your computer and use it in GitHub Desktop.
PlaceholderAttributeMixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PlaceholderAttributeMixin(object): | |
def __init__(self, *args, **kwargs): | |
super(PlaceholderAttributeMixin, self).__init__(*args, **kwargs) | |
for field_name in self.fields: | |
field = self.fields.get(field_name) | |
if field: | |
if type(field.widget) in (forms.TextInput, forms.DateInput): | |
field.widget = forms.TextInput(attrs={'placeholder': field.label}) | |
elif type(field.widget) in (forms.PasswordInput,): | |
field.widget = forms.PasswordInput(attrs={'placeholder': field.label}) | |
# Usage: | |
from registration.forms import RegistrationForm | |
from helpers.forms import PlaceholderAttributeMixin | |
class PlaceholderRegistrationForm(PlaceholderAttributeMixin, RegistrationForm): | |
pass | |
## PlaceholderRegistrationForm now inserts html5 placeholder attribute in all text, date and password inputs. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment