fix: 手机号可以为空及验证逻辑修改

pull/10176/head
jiangweidong 2 years ago committed by Jiangjie.Bai
parent b58488a7e9
commit 8ff1bae7e6

@ -10,6 +10,7 @@ from rest_framework.validators import (
UniqueTogetherValidator, ValidationError
)
from rest_framework import serializers
from phonenumbers.phonenumberutil import NumberParseException
from common.utils.strings import no_special_chars
@ -47,6 +48,11 @@ class PhoneValidator:
message = _('The mobile phone number format is incorrect')
def __call__(self, value):
phone = phonenumbers.parse(value)
if not phonenumbers.is_valid_number(phone):
try:
phone = phonenumbers.parse(value)
valid = phonenumbers.is_valid_number(phone)
except NumberParseException:
valid = False
if valid:
raise serializers.ValidationError(self.message)

@ -106,7 +106,7 @@ class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializer
allow_null=True, max_length=1024,
)
phone = PhoneField(
validators=[PhoneValidator()], required=False, allow_null=True, label=_("Phone")
validators=[PhoneValidator()], required=False, allow_blank=True, allow_null=True, label=_("Phone")
)
custom_m2m_fields = {
"system_roles": [BuiltinRole.system_user],

Loading…
Cancel
Save