class OpportunitySearchForm(forms.Form): sector = forms.ChoiceField(label=_('sector'), widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-sector'}, use_nice_ids=True, ), required=False) scale = forms.ChoiceField(label=_('scale'), widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-scale'}, use_nice_ids=True, ), required=False) region = forms.ChoiceField(label=_('region'), widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-region'}, use_nice_ids=True, ), required=False) sub_sector = forms.ChoiceField( label=_('sub_sector'), widget=forms.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-sub_sector'}, use_nice_ids=True, ), required=False) sort_by = forms.ChoiceField( label=_('sort_by'), widget=Select(attrs={'onchange': 'this.form.submit()'}), required=False, ) def __init__(self, sectors, scales, regions, sort_by_options, sub_sectors, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['sector'].choices = sectors self.fields['scale'].choices = scales self.fields['region'].choices = regions self.fields['sort_by'].choices = sort_by_options self.fields['sub_sector'].choices = sub_sectors
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)
def test_checkbox_inline_label_multiple_widget(): widget = forms.CheckboxSelectInlineLabelMultiple() html = widget.render('name', 'value') assert '<ul ' in html