mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-17 18:09:25 +02:00
Validation logic
This commit is contained in:
parent
860f8f4e3c
commit
888d77bf96
5 changed files with 63 additions and 51 deletions
|
@ -2400,6 +2400,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||||
// Within the suborganization section, we also have a div that contains orgname, city, and stateterritory
|
// Within the suborganization section, we also have a div that contains orgname, city, and stateterritory
|
||||||
const suborganizationDetailsFieldset = document.querySelector("#requesting-entity-fieldset__suborganization__details");
|
const suborganizationDetailsFieldset = document.querySelector("#requesting-entity-fieldset__suborganization__details");
|
||||||
|
|
||||||
|
var isCustomSuborganization = document.querySelector("#id_portfolio_requesting_entity-is_custom_suborganization")
|
||||||
|
|
||||||
// Use a variable to determine which option has been selected on the yes/no form.
|
// Use a variable to determine which option has been selected on the yes/no form.
|
||||||
|
|
||||||
// Don't do anything if we are missing crucial page elements
|
// Don't do anything if we are missing crucial page elements
|
||||||
|
@ -2421,8 +2423,10 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||||
// We should hide the org name fields when we select the special other value
|
// We should hide the org name fields when we select the special other value
|
||||||
if (subOrgSelect.value === "other") {
|
if (subOrgSelect.value === "other") {
|
||||||
showElement(suborganizationDetailsFieldset);
|
showElement(suborganizationDetailsFieldset);
|
||||||
|
isCustomSuborganization.value = "True";
|
||||||
} else {
|
} else {
|
||||||
hideElement(suborganizationDetailsFieldset);
|
hideElement(suborganizationDetailsFieldset);
|
||||||
|
isCustomSuborganization.value = "False";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2434,6 +2438,10 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||||
subOrgSelect.add(fakeOption);
|
subOrgSelect.add(fakeOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isCustomSuborganization.value === "True") {
|
||||||
|
subOrgSelect.value = "other"
|
||||||
|
}
|
||||||
|
|
||||||
// Add event listener to is_suborganization radio buttons
|
// Add event listener to is_suborganization radio buttons
|
||||||
isSuborgRadios.forEach(radio => {
|
isSuborgRadios.forEach(radio => {
|
||||||
// Run this here for initial display.
|
// Run this here for initial display.
|
||||||
|
|
|
@ -61,6 +61,9 @@ class RequestingEntityForm(RegistrarForm):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Add a hidden field to store that we are adding a custom suborg
|
||||||
|
is_custom_suborganization = forms.BooleanField(required=False, widget=forms.HiddenInput())
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -68,52 +71,52 @@ class RequestingEntityForm(RegistrarForm):
|
||||||
self.fields["sub_organization"].queryset = Suborganization.objects.filter(portfolio=self.domain_request.portfolio)
|
self.fields["sub_organization"].queryset = Suborganization.objects.filter(portfolio=self.domain_request.portfolio)
|
||||||
|
|
||||||
def clean_sub_organization(self):
|
def clean_sub_organization(self):
|
||||||
"""Require something to be selected when this is a federal agency."""
|
sub_organization = self.cleaned_data.get("sub_organization")
|
||||||
is_suborganization = self.cleaned_data.get("is_suborganization", None)
|
is_custom = self.cleaned_data.get("is_custom_suborganization")
|
||||||
sub_organization = self.cleaned_data.get("sub_organization", None)
|
print(f"in clean: {sub_organization}")
|
||||||
print(f"sub org is: {sub_organization} vs is_suborg: {is_suborganization}")
|
if is_custom:
|
||||||
if is_suborganization and not sub_organization:
|
# If it's a custom suborganization, return None (equivalent to selecting nothing)
|
||||||
raise forms.ValidationError(
|
return None
|
||||||
"Select a suborganization.",
|
|
||||||
code="required",
|
|
||||||
)
|
|
||||||
return sub_organization
|
return sub_organization
|
||||||
|
|
||||||
def clean_requested_suborganization(self):
|
def full_clean(self):
|
||||||
field = self.cleaned_data.get("requested_suborganization")
|
# Remove the custom other field before cleaning
|
||||||
if self.is_custom_suborg() and not field:
|
data = self.data.copy() if self.data else None
|
||||||
raise forms.ValidationError(
|
suborganization = self.data.get('portfolio_requesting_entity-sub_organization')
|
||||||
"Enter details for your organization name.",
|
is_suborganization = self.data.get("portfolio_requesting_entity-is_suborganization")
|
||||||
code="required",
|
if suborganization:
|
||||||
)
|
if "other" in data['portfolio_requesting_entity-sub_organization']:
|
||||||
return field
|
# Remove the 'other' value
|
||||||
|
data['portfolio_requesting_entity-sub_organization'] = ""
|
||||||
|
|
||||||
|
# Set the modified data back to the form
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
# Call the parent's full_clean method
|
||||||
|
super().full_clean()
|
||||||
|
|
||||||
def clean_suborganization_city(self):
|
def clean(self):
|
||||||
field = self.cleaned_data.get("suborganization_city")
|
"""Custom clean implementation to handle our desired logic flow for suborganization.
|
||||||
if self.is_custom_suborg():
|
Given that these fields often corely on eachother, we need to do this in the parent function."""
|
||||||
if not field:
|
cleaned_data = super().clean()
|
||||||
raise forms.ValidationError(
|
|
||||||
"Enter details for your city.",
|
|
||||||
code="required",
|
|
||||||
)
|
|
||||||
return field
|
|
||||||
|
|
||||||
def clean_suborganization_state_territory(self):
|
suborganization = self.cleaned_data.get("sub_organization")
|
||||||
field = self.cleaned_data.get("suborganization_state_territory")
|
|
||||||
if self.is_custom_suborg():
|
|
||||||
if not field:
|
|
||||||
raise forms.ValidationError(
|
|
||||||
"Enter details for your state or territory.",
|
|
||||||
code="required",
|
|
||||||
)
|
|
||||||
return field
|
|
||||||
|
|
||||||
def is_custom_suborg(self):
|
|
||||||
"""Checks that the select suborg is 'other', which is a custom field indicating
|
|
||||||
that we should create a new suborganization."""
|
|
||||||
is_suborganization = self.cleaned_data.get("is_suborganization")
|
is_suborganization = self.cleaned_data.get("is_suborganization")
|
||||||
sub_organization = self.cleaned_data.get("sub_organization")
|
is_custom_suborganization = self.cleaned_data.get("is_custom_suborganization")
|
||||||
return is_suborganization and sub_organization == "other"
|
if is_suborganization:
|
||||||
|
if is_custom_suborganization:
|
||||||
|
# Validate custom suborganization fields
|
||||||
|
if not cleaned_data.get("requested_suborganization"):
|
||||||
|
self.add_error("requested_suborganization", "Enter details for your organization name.")
|
||||||
|
if not cleaned_data.get("suborganization_city"):
|
||||||
|
self.add_error("suborganization_city", "Enter details for your city.")
|
||||||
|
if not cleaned_data.get("suborganization_state_territory"):
|
||||||
|
self.add_error("suborganization_state_territory", "Enter details for your state or territory.")
|
||||||
|
elif not suborganization:
|
||||||
|
self.add_error("sub_organization", "Select a suborganization.")
|
||||||
|
|
||||||
|
cleaned_data = super().clean()
|
||||||
|
return cleaned_data
|
||||||
|
|
||||||
|
|
||||||
class RequestingEntityYesNoForm(BaseYesNoForm):
|
class RequestingEntityYesNoForm(BaseYesNoForm):
|
||||||
|
|
|
@ -1210,6 +1210,12 @@ class DomainRequest(TimeStampedModel):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def is_custom_suborganization(self) -> bool:
|
||||||
|
if self.is_suborganization():
|
||||||
|
return not self.sub_organization and self.has_information_required_to_make_suborganization()
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def has_information_required_to_make_suborganization(self):
|
def has_information_required_to_make_suborganization(self):
|
||||||
"""Checks if we have all the information we need to create a new suborganization object.
|
"""Checks if we have all the information we need to create a new suborganization object.
|
||||||
Checks for a the existence of requested_suborganization, suborganization_city, suborganization_state_territory"""
|
Checks for a the existence of requested_suborganization, suborganization_city, suborganization_state_territory"""
|
||||||
|
|
|
@ -12,13 +12,17 @@
|
||||||
<h2>Who will use the domain you’re requesting?</h2>
|
<h2>Who will use the domain you’re requesting?</h2>
|
||||||
</legend>
|
</legend>
|
||||||
|
|
||||||
|
<p class="margin-bottom-0 margin-top-1">
|
||||||
|
<em>Select one. <abbr class="usa-hint usa-hint--required" title="required">*</abbr></em>
|
||||||
|
</p>
|
||||||
|
|
||||||
{# forms.0 is a small yes/no form that toggles the visibility of "requesting entity" formset #}
|
{# forms.0 is a small yes/no form that toggles the visibility of "requesting entity" formset #}
|
||||||
{% with add_class="usa-radio__input--tile" add_legend_class="usa-sr-only" %}
|
{% with add_class="usa-radio__input--tile" add_legend_class="usa-sr-only" %}
|
||||||
{% with attr_required=True %}
|
{% with attr_required=True %}
|
||||||
{% input_with_errors forms.0.is_suborganization %}
|
{% input_with_errors forms.0.is_suborganization %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
|
{% input_with_errors forms.1.is_custom_suborganization %}
|
||||||
<div id="requesting-entity-fieldset__suborganization" class="margin-top-4">
|
<div id="requesting-entity-fieldset__suborganization" class="margin-top-4">
|
||||||
<h2>Add suborganization information</h2>
|
<h2>Add suborganization information</h2>
|
||||||
<p>
|
<p>
|
||||||
|
|
|
@ -602,15 +602,6 @@ class RequestingEntity(DomainRequestWizard):
|
||||||
if is_suborganization and (sub_organization or requested_suborganization):
|
if is_suborganization and (sub_organization or requested_suborganization):
|
||||||
# Cleanup the organization name field, as this isn't for suborganizations.
|
# Cleanup the organization name field, as this isn't for suborganizations.
|
||||||
self.domain_request.organization_name = None
|
self.domain_request.organization_name = None
|
||||||
|
|
||||||
# Create or get the Suborganization.
|
|
||||||
# Then update the domain_request with the new or existing suborganization
|
|
||||||
if not sub_organization:
|
|
||||||
sub_organization, created = Suborganization.objects.get_or_create(
|
|
||||||
name=cleaned_data.get("requested_suborganization"),
|
|
||||||
portfolio=self.domain_request.portfolio,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.domain_request.sub_organization = sub_organization
|
self.domain_request.sub_organization = sub_organization
|
||||||
else:
|
else:
|
||||||
# If the user doesn't intend to create a suborg, simply don't make one and do some data cleanup
|
# If the user doesn't intend to create a suborg, simply don't make one and do some data cleanup
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue