mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-21 11:59:24 +02:00
Validation logic for /admin
This commit is contained in:
parent
de8d252136
commit
e1ad261e9c
3 changed files with 41 additions and 2 deletions
|
@ -90,7 +90,7 @@ class RequestingEntityForm(RegistrarForm):
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
"This suborganization already exists. "
|
"This suborganization already exists. "
|
||||||
"Choose a new name, or select it directly if you would like to use it."
|
"Choose a new name, or select it directly if you would like to use it."
|
||||||
)
|
)
|
||||||
return name
|
return name
|
||||||
|
|
||||||
def full_clean(self):
|
def full_clean(self):
|
||||||
|
|
|
@ -672,6 +672,46 @@ class DomainRequest(TimeStampedModel):
|
||||||
# Store original values for caching purposes. Used to compare them on save.
|
# Store original values for caching purposes. Used to compare them on save.
|
||||||
self._cache_status_and_status_reasons()
|
self._cache_status_and_status_reasons()
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
super().clean()
|
||||||
|
# Validation logic for a suborganization request
|
||||||
|
if self.is_requesting_new_suborganization():
|
||||||
|
# Raise an error if this suborganization already exists
|
||||||
|
Suborganization = apps.get_model("registrar.Suborganization")
|
||||||
|
if (
|
||||||
|
self.requested_suborganization
|
||||||
|
and Suborganization.objects.filter(
|
||||||
|
name__iexact=self.requested_suborganization,
|
||||||
|
portfolio=self.portfolio,
|
||||||
|
name__isnull=False,
|
||||||
|
portfolio__isnull=False,
|
||||||
|
).exists()
|
||||||
|
):
|
||||||
|
raise ValidationError(
|
||||||
|
"This suborganization already exists. "
|
||||||
|
"Choose a new name, or select it directly if you would like to use it."
|
||||||
|
)
|
||||||
|
elif self.portfolio and not self.sub_organization:
|
||||||
|
# You cannot create a new suborganization without these fields
|
||||||
|
required_suborg_fields = {
|
||||||
|
"requested_suborganization": self.requested_suborganization,
|
||||||
|
"suborganization_city": self.suborganization_city,
|
||||||
|
"suborganization_state_territory": self.suborganization_state_territory,
|
||||||
|
}
|
||||||
|
|
||||||
|
if any(bool(value) for value in required_suborg_fields.values()):
|
||||||
|
# Find which fields are empty
|
||||||
|
errors_dict = {
|
||||||
|
field_name: [f"This field is required when creating a new suborganization."]
|
||||||
|
for field_name, value in required_suborg_fields.items()
|
||||||
|
if not value
|
||||||
|
}
|
||||||
|
# Adds a validation error to each missing field
|
||||||
|
raise ValidationError({
|
||||||
|
k: ValidationError(v, code='required')
|
||||||
|
for k, v in errors_dict.items()
|
||||||
|
})
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
"""Save override for custom properties"""
|
"""Save override for custom properties"""
|
||||||
self.sync_organization_type()
|
self.sync_organization_type()
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from registrar.models.domain_request import DomainRequest
|
from registrar.models.domain_request import DomainRequest
|
||||||
from .utility.time_stamped_model import TimeStampedModel
|
from .utility.time_stamped_model import TimeStampedModel
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue