diff --git a/resrc/userprofile/forms.py b/resrc/userprofile/forms.py index 073d54c..2544067 100644 --- a/resrc/userprofile/forms.py +++ b/resrc/userprofile/forms.py @@ -6,24 +6,38 @@ from django.contrib.auth import authenticate from crispy_forms.helper import FormHelper -from crispy_forms_foundation.layout import Layout, Row, Div, Fieldset, Submit, Field, HTML +from crispy_forms_foundation.layout import Layout, Row, Column, Div, Fieldset, Submit, Field, HTML from captcha.fields import CaptchaField from django.conf import settings +class AbideCrispyField(Field): + template = 'abide_crispy_field.html' + + def __init__(self, *args, **kwargs): + clientside_error = kwargs.pop('clientside_error', None) + super(AbideCrispyField, self).__init__(*args, **kwargs) + self.clientside_error = clientside_error + + def render(self, form, form_style, context, *args, **kwargs): + if self.clientside_error is not None: + context['clientside_error'] = self.clientside_error + return super(AbideCrispyField, self).render(form, form_style, context, *args, **kwargs) class LoginForm(forms.Form): username = forms.CharField(max_length=30) password = forms.CharField(max_length=76, widget=forms.PasswordInput) - class RegisterForm(forms.Form): - email = forms.EmailField(label='email') - username = forms.CharField(label='username', max_length=30) + email = forms.EmailField(label='email', widget=forms.TextInput(attrs={'required':''})) + username = forms.CharField(label='username', max_length=30, widget=forms.TextInput(attrs={'required':''}) + ) password = forms.CharField( - label='password', max_length=76, widget=forms.PasswordInput) + label='password', max_length=76, widget=forms.PasswordInput(attrs={'required':''}) + ) password_confirm = forms.CharField( - label='confirm password', max_length=76, widget=forms.PasswordInput) + label='confirm password', max_length=76, widget=forms.PasswordInput(attrs={'required':''}) + ) captcha = CaptchaField() def __init__(self, *args, **kwargs): @@ -31,54 +45,31 @@ def __init__(self, *args, **kwargs): self.helper.form_method = 'post' self.helper.layout = Layout( - HTML('\ -
\ -
\ - \ - \ - 3-30 characters, a-z A-Z 0-9 _ .\ -
\ -
\ - \ - \ - A valid email address is required.\ -
\ -
\ - \ -
\ -
\ - \ - \ -
\ -
\ - \ - \ - password mismatch\ -
\ -
\ - \ -
\ -
\ - '), - Field('captcha'), - HTML('\ -
\ -
\ - \ - Cancel\ -
\ -
\ - \ - \ - ') + Row( + Column( + AbideCrispyField('username', pattern='username', clientside_error='3-30 characters a-z A-Z 0-9 _ .'), + css_class='large-6' + ), + Column( + AbideCrispyField('email', clientside_error='A valid email address is required.'), + css_class='large-6' + ) + ), + Row( + Column(AbideCrispyField('password', clientside_error='A password is required.'), css_class='large-6'), + Column( + AbideCrispyField('password_confirm', pattern='pass_confirm', clientside_error='Password mismatch.'), + css_class='large-6' + ) + ), + Row( + Column(AbideCrispyField('captcha'), css_class='large-2'), + Column( + Submit('submit', 'Register'), + HTML('Cancel'), + css_class='large-4' + ) + ) ) super(RegisterForm, self).__init__(*args, **kwargs) diff --git a/templates/abide_crispy_field.html b/templates/abide_crispy_field.html new file mode 100644 index 0000000..d55f420 --- /dev/null +++ b/templates/abide_crispy_field.html @@ -0,0 +1,50 @@ +{% load crispy_forms_field %} + +{% if field.is_hidden %} + {{ field }} +{% else %} +
{% spaceless %} + + {% if field.label %} + {% if field|is_checkbox %} + {% crispy_field field %} + {% endif %} + + + {% endif %} + + {% if field|is_checkboxselectmultiple %} + {% load l10n %} + + {% endif %} + + {% if not field|is_checkbox and not field|is_checkboxselectmultiple %} + {% crispy_field field %} + {% endif %} + + {% if form_show_errors %} + {% for error in field.errors %} + + {{ error }} + + {% endfor %} + {% endif %} + + {% if clientside_error %} + + {{ clientside_error }} + + {% endif %} + + {% if field.help_text %} +
{{ field.help_text|safe }}
+ {% endif %} + {% endspaceless %}
+{% endif %} diff --git a/templates/base.html b/templates/base.html index a107962..6c5af0e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -130,7 +130,16 @@

reSRC

{% block last_body %}{% endblock %}