【发布时间】:2017-07-03 05:04:12
【问题描述】:
我正在尝试在网站上使用电子邮件和电话进行注册。用户可以使用电话或电子邮件或两者注册。如果用户将电话和电子邮件字段都留空,则会引发 ValidationError,“您不能将电话和电子邮件字段都留空。您必须至少填写其中一个字段。”
我们为username, email, phone, password 提供了单独的clean 方法。我不想在save() 上实现上述验证。我也不想在 User 模型中定义 clean 方法。
我已经为此表格编写了测试,并且它们通过了。但是如果我同时使用clean 和clean_fieldname 可能会出现什么错误?使用视图时会不会成为问题?
我有 3 个问题:
- 我可以同时使用
clean_fieldname和clean方法吗? 表格? - 我可以通过什么其他方式确保用户至少注册 电话还是电子邮件?
-
clean()和validate()如何工作?我已经阅读了 django 文档,但我并不完全理解它。
这是我实现的代码。
class RegisterForm(SanitizeFieldsForm, forms.ModelForm):
email = forms.EmailField(required=False)
message = _("Phone must have format: +9999999999. Upto 15 digits allowed."
" Do not include hyphen or blank spaces in between, at the"
" beginning or at the end.")
phone = forms.RegexField(regex=r'^\+(?:[0-9]?){6,14}[0-9]$',
error_messages={'invalid': message},
required=False)
password = forms.CharField(widget=forms.PasswordInput())
MIN_LENGTH = 10
class Meta:
model = User
fields = ['username', 'email', 'phone', 'password',
'full_name']
class Media:
js = ('js/sanitize.js', )
def clean(self):
super(RegisterForm, self).clean()
email = self.data.get('email')
phone = self.data.get('phone')
if (not phone) and (not email):
raise forms.ValidationError(
_("You cannot leave both phone and email empty."
" Signup with either phone or email or both."))
def clean_username(self):
username = self.data.get('username')
check_username_case_insensitive(username)
if username.lower() in settings.CADASTA_INVALID_ENTITY_NAMES:
raise forms.ValidationError(
_("Username cannot be “add” or “new”."))
return username
def clean_password(self):
password = self.data.get('password')
validate_password(password)
errors = []
email = self.data.get('email')
if email:
email = email.split('@')
if email[0].casefold() in password.casefold():
errors.append(_("Passwords cannot contain your email."))
username = self.data.get('username')
if len(username) and username.casefold() in password.casefold():
errors.append(
_("The password is too similar to the username."))
phone = self.data.get('phone')
if phone:
if phone_validator(phone):
phone = str(parse_phone(phone).national_number)
if phone in password:
errors.append(_("Passwords cannot contain your phone."))
if errors:
raise forms.ValidationError(errors)
return password
def clean_email(self):
email = self.data.get('email')
if email:
if User.objects.filter(email=email).exists():
raise forms.ValidationError(
_("Another user with this email already exists"))
return email
def clean_phone(self):
phone = self.data.get('phone')
if phone:
if User.objects.filter(phone=phone).exists():
raise forms.ValidationError(
_("Another user with this phone already exists"))
return phone
def save(self, *args, **kwargs):
user = super().save(*args, **kwargs)
user.set_password(self.cleaned_data['password'])
user.save()
return user
【问题讨论】:
标签: django validation django-forms modelform