class MultiSelectAutoCompleteForm(forms.Form): regions = choices.EXPERTISE_REGION_CHOICES industries = choices.INDUSTRIES region = forms.MultipleChoiceField( label='What region?', help_text='For UK businesses only', choices=regions, widget=forms.SelectMultipleAutocomplete, ) industry = forms.MultipleChoiceField( label='What industry?', choices=industries, widget=forms.SelectMultipleAutocomplete, )
class LanguageExpertiseForm(forms.Form): expertise_languages = forms.MultipleChoiceField( label='Select the languages you have expertise in', choices=choices.EXPERTISE_LANGUAGES, required=False, widget=SelectMultiple(attrs={'placeholder': 'Please select'}), )
class IndustryExpertiseForm(forms.Form): expertise_industries = forms.MultipleChoiceField( label='Choose the industries you work with', choices=choices.INDUSTRIES, required=False, widget=SelectMultiple(attrs={'placeholder': 'Please select'}), )
class CountryExpertiseForm(forms.Form): expertise_countries = forms.MultipleChoiceField( label='Select the countries you have expertise in', choices=choices.COUNTRY_CHOICES, required=False, widget=SelectMultiple(attrs={'placeholder': 'Please select'}), )
class RegionalExpertiseForm(forms.Form): expertise_regions = forms.MultipleChoiceField( label='Select the regions you have expertise in', choices=choices.EXPERTISE_REGION_CHOICES, required=False, widget=SelectMultiple(attrs={'placeholder': 'Please select'}), )
class MarketPriceChangeForm(forms.Form): market_price_changed_type = forms.MultipleChoiceField( label='', help_text=HELP_TEXT_SELECT_CHANGE_TYPE, choices=CHOICES_CHANGE_TYPE_PRICE, widget=forms.CheckboxSelectInlineLabelMultiple, ) market_price_change_comment = forms.CharField( label="Tell us more", widget=Textarea(attrs={'rows': 6}), )
class OtherChangesForm(forms.Form): has_other_changes_type = forms.MultipleChoiceField( label='', help_text=HELP_TEXT_SELECT_CHANGE_TYPE, choices=CHOICES_CHANGE_TYPE, widget=forms.CheckboxSelectInlineLabelMultiple, ) other_changes_comment = forms.CharField( label="Tell us more", widget=Textarea(attrs={'rows': 6}), )
class MultipleChoiceForm(PrefixIdMixin, forms.Form): multiple_choice = forms.MultipleChoiceField( label='Q1: Multiple choice checkboxes', help_text='This is some help text.', widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=( ('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'), ), )
class CategoryForm(forms.Form): error_css_class = 'input-field-container has-error' CATEGORY_CHOICES = ( 'Securing upfront funding', 'Offering competitive but secure payment terms', 'Guidance on export finance and insurance', ) categories = forms.MultipleChoiceField( label='', widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=((choice, choice) for choice in CATEGORY_CHOICES))
class CompanyDetailsForm(forms.Form): EXPORT_CHOICES = ( 'I have three years of registered accounts', 'I have customers outside UK', 'I supply companies that sell overseas', ) INDUSTRY_CHOICES = [('', '')] + [(value.replace('_', ' ').title(), label) for (value, label) in choices.INDUSTRIES ] + [('Other', 'Other')] error_css_class = 'input-field-container has-error' trading_name = forms.CharField(label='Registered name') company_number = forms.CharField(label='Companies House number', required=False) address_line_one = forms.CharField(label='Building and street') address_line_two = forms.CharField(label='', required=False) address_town_city = forms.CharField(label='Town or city') address_county = forms.CharField(label='County') address_post_code = forms.CharField(label='Postcode') industry = forms.ChoiceField(initial='thing', choices=INDUSTRY_CHOICES) industry_other = forms.CharField( label='Type in your industry', widget=TextInput(attrs={'class': 'js-field-other'}), required=False, ) export_status = forms.MultipleChoiceField( label='', widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=((choice, choice) for choice in EXPORT_CHOICES), ) def clean(self): cleaned_data = super().clean() return { **cleaned_data, 'not_companies_house': not cleaned_data.get('company_number') }
class DemoFormErrors(PrefixIdMixin, forms.Form): text_field1 = forms.CharField(label='Simple text field', help_text='Some help text', required=True) checkbox1 = forms.BooleanField( label='Label text', required=True, help_text=('Some help text.'), widget=forms.CheckboxWithInlineLabel(attrs={'id': 'checkbox-one'})) multiple_choice = forms.MultipleChoiceField( label='Multiple choice checkboxes', required=True, help_text='Some help text.', widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=( ('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'), ), ) radio = forms.ChoiceField( label='Radio select', required=True, label_suffix='', help_text='Some help text.', widget=forms.RadioSelect(use_nice_ids=True, attrs={'id': 'radio-one'}), choices=( (True, 'Yes'), (False, 'No'), )) def clean(self, *args, **kwargs): self.add_error( field=None, error=['Some non-field error', 'Some other non-field error']) super().clean(*args, **kwargs)
class ConsentFieldMixin(forms.Form): contact_consent = forms.MultipleChoiceField( label=render_to_string('core/contact-consent.html', {'privacy_url': PRIVACY_POLICY_URL}), widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True), choices=CONSENT_CHOICES, required=False, ) @staticmethod def move_to_end(fields, name): fields.remove(name) fields.append(name) def order_fields(self, field_order): # move terms agreed and captcha to the back field_order = field_order or list(self.fields.keys()) field_order = field_order[:] self.move_to_end(fields=field_order, name='contact_consent') if 'captcha' in field_order: self.move_to_end(fields=field_order, name='captcha') return super().order_fields(field_order)
class HighPotentialOpportunityForm(forms.Form): action_class = GovNotifyAction COMPANY_SIZE_CHOICES = [ ('1 - 10', '1 - 10'), ('11 - 50', '11 - 50'), ('51 - 250', '51 - 250'), ('250+', '250+'), ] REQUIRED_USER_UTM_DATA_FIELD_NAMES = ( 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', ) def __init__(self, field_attributes, opportunity_choices, utm_data=None, *args, **kwargs): for field_name, field in self.base_fields.items(): attributes = field_attributes.get(field_name) if attributes: field.__dict__.update(attributes) self.base_fields['opportunities'].choices = opportunity_choices self.utm_data = utm_data or {} # set empty string by default not exists data fields for field_name in self.REQUIRED_USER_UTM_DATA_FIELD_NAMES: self.utm_data.setdefault(field_name, '') return super().__init__(*args, **kwargs) full_name = forms.CharField() role_in_company = forms.CharField() email_address = forms.EmailField() phone_number = forms.CharField() company_name = forms.CharField() website_url = forms.CharField(required=False) country = forms.ChoiceField( choices=[('', 'Please select')] + choices.COUNTRY_CHOICES, widget=Select(attrs={'id': 'js-country-select'}), ) company_size = forms.ChoiceField(choices=COMPANY_SIZE_CHOICES) opportunities = forms.MultipleChoiceField( widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=[] # set in __init__ ) comment = forms.CharField(widget=Textarea, required=False) terms_agreed = forms.BooleanField(label=mark_safe( 'Tick this box to accept the ' f'<a href="{urls.TERMS_AND_CONDITIONS}" target="_blank">terms and ' 'conditions</a> of the great.gov.uk service.')) captcha = ReCaptchaField( label='', label_suffix='', ) @property def serialized_data(self): formatted_opportunities = [ '• {opportunity[1]}: {opportunity[0]}'.format(opportunity=item) for item in self.base_fields['opportunities'].choices if item[0] in self.cleaned_data['opportunities'] ] return { **self.cleaned_data, **self.utm_data, 'opportunity_urls': '\n'.join(formatted_opportunities), } def send_agent_email(self, form_url): sender = Sender(email_address=self.cleaned_data['email_address'], country_code=self.cleaned_data['country']) action = self.action_class( template_id=settings.HPO_GOV_NOTIFY_AGENT_TEMPLATE_ID, email_address=settings.HPO_GOV_NOTIFY_AGENT_EMAIL_ADDRESS, form_url=form_url, sender=sender, ) response = action.save(self.serialized_data) response.raise_for_status() def send_user_email(self, form_url): # no need to set `sender` as this is just a confirmation email. action = self.action_class( template_id=settings.HPO_GOV_NOTIFY_USER_TEMPLATE_ID, email_address=self.cleaned_data['email_address'], form_url=form_url, email_reply_to_id=settings.HPO_GOV_NOTIFY_USER_REPLY_TO_ID, ) response = action.save(self.serialized_data) response.raise_for_status() def save(self, form_url): self.send_agent_email(form_url=form_url) self.send_user_email(form_url=form_url)
class ProblemDetailsForm(forms.Form): error_css_class = 'input-field-container has-error' location = forms.ChoiceField( choices=LOCATION_CHOICES, label='Where are you trying to export to or invest in?', error_messages={ 'required': ('Tell us where you are trying to export to or invest in') }) product_service = forms.CharField( label='What goods or services do you want to export?', help_text='Or tell us about an investment you want to make', error_messages={ 'required': ('Tell us what you’re trying to export or invest in') }) problem_summary = forms.CharField( label=mark_safe(('<p>Tell us about your problem, including: </p>' '<ul class="list list-bullet">' '<li>what’s affecting your export or investment</li>' '<li>when you became aware of the problem</li>' '<li>how you became aware of the problem</li>' '<li>if this has happened before</li>' '<li>' 'any information you’ve been given or ' 'correspondence you’ve had' '</li>' '<li>' 'the HS (Harmonized System) code for your goods, ' 'if you know it' '</li>' '</ul>')), widget=Textarea, error_messages={'required': 'Tell us about the problem you’re facing'}) impact = forms.CharField( label=('How has the problem affected your business or ' 'industry, or how could it affect it?'), widget=Textarea, error_messages={ 'required': ('Tell us how your business or industry ' 'is being affected by the problem') }) resolve_summary = forms.CharField( label=mark_safe(('<p>Tell us about any steps you’ve taken ' 'to resolve the problem, including: </p>' '<ul class="list list-bullet">' '<li>people you’ve contacted</li>' '<li>when you contacted them</li>' '<li>what happened</li>' '</ul>')), widget=Textarea, error_messages={ 'required': ('Tell us what you’ve done to resolve your ' 'problem, even if this is your first step') }) problem_cause = forms.MultipleChoiceField( label='Is the problem caused by or related to any of the following?', widget=widgets.TickboxWithOptionsHelpText( use_nice_ids=True, attrs={ 'id': 'radio-one', 'help_text': { 'radio-one-covid-19': 'Problem related to the COVID-19 (coronavirus) pandemic.', } }, ), choices=PROBLEM_CAUSE_CHOICES, required=False, ) def clean_location(self): value = self.cleaned_data['location'] self.cleaned_data['location_label'] = LOCATION_MAP[value] return value def clean_problem_cause(self): value = self.cleaned_data['problem_cause'] self.cleaned_data['problem_cause_label'] = [ PROBLEM_CAUSE_MAP[item] for item in value ] return value