updated combobox widget to set proper data-default-value and set domain suborganization form to use new combobox widget

This commit is contained in:
David Kennedy 2025-01-06 14:15:13 -05:00
parent f359a636d0
commit 4480e32553
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
3 changed files with 4 additions and 32 deletions

View file

@ -4,6 +4,7 @@ import logging
from django import forms
from django.core.validators import MinValueValidator, MaxValueValidator, RegexValidator, MaxLengthValidator
from django.forms import formset_factory
from registrar.forms.utility.combobox import ComboboxWidget
from registrar.models import DomainRequest, FederalAgency
from phonenumber_field.widgets import RegionalPhoneNumberWidget
from registrar.models.suborganization import Suborganization
@ -161,9 +162,10 @@ class DomainSuborganizationForm(forms.ModelForm):
"""Form for updating the suborganization"""
sub_organization = forms.ModelChoiceField(
label = "Suborganization name",
queryset=Suborganization.objects.none(),
required=False,
widget=forms.Select(),
widget=ComboboxWidget,
)
class Meta:
@ -178,20 +180,6 @@ class DomainSuborganizationForm(forms.ModelForm):
portfolio = self.instance.portfolio if self.instance else None
self.fields["sub_organization"].queryset = Suborganization.objects.filter(portfolio=portfolio)
# Set initial value
if self.instance and self.instance.sub_organization:
self.fields["sub_organization"].initial = self.instance.sub_organization
# Set custom form label
self.fields["sub_organization"].label = "Suborganization name"
# Use the combobox rather than the regular select widget
self.fields["sub_organization"].widget.template_name = "django/forms/widgets/combobox.html"
# Set data-default-value attribute
if self.instance and self.instance.sub_organization:
self.fields["sub_organization"].widget.attrs["data-default-value"] = self.instance.sub_organization.pk
class BaseNameserverFormset(forms.BaseFormSet):
def clean(self):

View file

@ -298,23 +298,6 @@ class OrganizationContactForm(RegistrarForm):
label="Urbanization (required for Puerto Rico only)",
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Initialize federal_agency combobox widget
# Domain requests forms have prefix associated with step
prefix = kwargs.get("prefix", "")
prefixed_name = f"{prefix}-federal_agency" if prefix else "federal_agency"
# For combobox widget, need to set the data-default-value to selected value
if self.is_bound and self.data.get(prefixed_name):
# If form is bound (from a POST), use submitted value
self.fields["federal_agency"].widget.attrs["data-default-value"] = self.data.get(prefixed_name)
elif self.domain_request and self.domain_request.federal_agency:
# If form is not bound, set initial
self.fields["federal_agency"].initial = self.domain_request.federal_agency
self.fields["federal_agency"].widget.attrs["data-default-value"] = self.domain_request.federal_agency.pk
def clean_federal_agency(self):
"""Require something to be selected when this is a federal agency."""
federal_agency = self.cleaned_data.get("federal_agency", None)

View file

@ -11,6 +11,7 @@ for now we just carry the attribute to both the parent element and the select.
{{ name }}="{{ value }}"
{% endif %}
{% endfor %}
data-default-value="{% for group_name, group_choices, group_index in widget.optgroups %}{% for option in group_choices %}{% if option.selected %}{{ option.value }}{% endif %}{% endfor %}{% endfor %}"
>
{% include "django/forms/widgets/select.html" %}
</div>