Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ДОПЪЛНИТЕЛНА ВАЛИДАЦИЯ НА ПАРОЛАТА НА USER (Server-side validation)
- class MyHolidayUserCreationForm(auth_forms.UserCreationForm):
- user = None
- password1 = forms.CharField(
- label="Password",
- strip=False,
- widget=forms.PasswordInput,
- validators=[validate_password],
- )
- password2 = forms.CharField(
- label="Password confirmation",
- widget=forms.PasswordInput,
- strip=False,
- )
- class Meta(auth_forms.UserCreationForm.Meta):
- model = UserModel
- fields = ('email',)
- ДОБАВЯМ МОЯ ВАЛИДАТОВ СЛЕД ВГРАДЕНИТЕ В settings.py - AUTH_PASSWORD_VALIDATORS:
- AUTH_PASSWORD_VALIDATORS = [
- {
- 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
- },
- {
- 'NAME': 'accounts.validators.validate_password',
- },
- ]
- valdators.py
- from django.core.exceptions import ValidationError
- def validate_password(password):
- has_letter = False
- has_digit = False
- has_underscore = False
- for char in password:
- if char.isalpha():
- has_letter = True
- elif char.isdigit():
- has_digit = True
- elif char == '_':
- has_underscore = True
- if not has_letter:
- raise ValidationError("Password must contain at least one letter.")
- if not has_digit:
- raise ValidationError("Password must contain at least one digit.")
- if not has_underscore:
- raise ValidationError("Password must contain at least one underscore.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement