2๐
โ
So instead of using the RegexField
I would probably either go with a CharField
and a customized clean
-method for the field, which handles the whitespace issue first and only checks the regex if the the whitespace check passed.
Or I would use a CharField
and assign a custom validator first, that checks for the whitespaces, and additionally a RegexValidator
Something along these lines:
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
def validate_whitespaces(value):
if ' ' in value:
raise ValidationError("Oops, a whitespace error occured.")
username = forms.CharField(
max_length=50,
validators=[
validate_whitespaces,
RegexValidator(
regex='^[\w.@+-]+$',
message='Only use valid chars ...'
)
]
)
๐คarie
Source:stackexchange.com