Merge pull request #3179 from alphagov/validate-service-and-org-name

Validate service and organisation name
This commit is contained in:
Pea (Malgorzata Tyczynska)
2019-11-20 13:37:10 +00:00
committed by GitHub
6 changed files with 87 additions and 3 deletions

View File

@@ -40,6 +40,7 @@ from app.main.validators import (
CsvFileValidator,
DoesNotStartWithDoubleZero,
LettersNumbersAndFullStopsOnly,
MustContainAlphanumericCharacters,
NoCommasInPlaceHolders,
OnlySMSCharacters,
ValidEmail,
@@ -547,7 +548,8 @@ class RenameServiceForm(StripWhitespaceForm):
name = StringField(
u'Service name',
validators=[
DataRequired(message='Cannot be empty')
DataRequired(message='Cannot be empty'),
MustContainAlphanumericCharacters()
])
@@ -555,7 +557,8 @@ class RenameOrganisationForm(StripWhitespaceForm):
name = StringField(
u'Organisation name',
validators=[
DataRequired(message='Cannot be empty')
DataRequired(message='Cannot be empty'),
MustContainAlphanumericCharacters()
])
@@ -662,7 +665,8 @@ class CreateServiceForm(StripWhitespaceForm):
name = StringField(
"Whats your service called?",
validators=[
DataRequired(message='Cannot be empty')
DataRequired(message='Cannot be empty'),
MustContainAlphanumericCharacters()
])
organisation_type = OrganisationTypeField('Who runs this service?')

View File

@@ -111,3 +111,18 @@ class DoesNotStartWithDoubleZero:
def __call__(self, form, field):
if field.data and field.data.startswith("00"):
raise ValidationError(self.message)
class MustContainAlphanumericCharacters:
regex = re.compile(r".*[a-zA-Z0-9].*[a-zA-Z0-9].*")
def __init__(
self,
message="Must include at least two alphanumeric characters"
):
self.message = message
def __call__(self, form, field):
if field.data and not re.match(self.regex, field.data):
raise ValidationError(self.message)