Fixed #30776 -- Restored max length validation on AuthenticationForm.UsernameField.

Regression in 5ceaf14686.

Thanks gopackgo90 for the report and Mariusz Felisiak for tests.
This commit is contained in:
Sam Reynolds 2019-09-17 15:21:50 +01:00 committed by Mariusz Felisiak
parent b1d6b35e14
commit 6c9778a58e
2 changed files with 5 additions and 1 deletions

View File

@ -191,7 +191,9 @@ class AuthenticationForm(forms.Form):
# Set the max length and label for the "username" field. # Set the max length and label for the "username" field.
self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD) self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
self.fields['username'].max_length = self.username_field.max_length or 254 username_max_length = self.username_field.max_length or 254
self.fields['username'].max_length = username_max_length
self.fields['username'].widget.attrs['maxlength'] = username_max_length
if self.fields['username'].label is None: if self.fields['username'].label is None:
self.fields['username'].label = capfirst(self.username_field.verbose_name) self.fields['username'].label = capfirst(self.username_field.verbose_name)

View File

@ -423,6 +423,7 @@ class AuthenticationFormTest(TestDataMixin, TestCase):
CustomEmailField.objects.create_user(**data) CustomEmailField.objects.create_user(**data)
form = AuthenticationForm(None, data) form = AuthenticationForm(None, data)
self.assertEqual(form.fields['username'].max_length, 255) self.assertEqual(form.fields['username'].max_length, 255)
self.assertEqual(form.fields['username'].widget.attrs.get('maxlength'), 255)
self.assertEqual(form.errors, {}) self.assertEqual(form.errors, {})
@override_settings(AUTH_USER_MODEL='auth_tests.IntegerUsernameUser') @override_settings(AUTH_USER_MODEL='auth_tests.IntegerUsernameUser')
@ -435,6 +436,7 @@ class AuthenticationFormTest(TestDataMixin, TestCase):
IntegerUsernameUser.objects.create_user(**data) IntegerUsernameUser.objects.create_user(**data)
form = AuthenticationForm(None, data) form = AuthenticationForm(None, data)
self.assertEqual(form.fields['username'].max_length, 254) self.assertEqual(form.fields['username'].max_length, 254)
self.assertEqual(form.fields['username'].widget.attrs.get('maxlength'), 254)
self.assertEqual(form.errors, {}) self.assertEqual(form.errors, {})
def test_username_field_label(self): def test_username_field_label(self):