Ejemplo n.º 1
0
class RadioForm(PrefixIdMixin, forms.Form):
    radio = forms.ChoiceField(
        label='Q1: Radio select',
        label_suffix='',
        help_text='Some help text.',
        widget=forms.RadioSelect(use_nice_ids=True, attrs={'id': 'radio-one'}),
        choices=(
            (True, 'Yes'),
            (False, 'No'),
        ))
    radio_group = forms.ChoiceField(
        label='Q2: Radio select with option groups',
        label_suffix='',
        help_text='Some help text.',
        widget=forms.RadioSelect(use_nice_ids=True, attrs={'id': 'radio-two'}),
        choices=(
            ('Colours', (
                ('red', 'Red'),
                ('green', 'Green'),
                ('blue', 'Blue'),
            )),
            ('Numbers', (
                ('4', 'Four'),
                ('5', 'Five'),
                ('6', 'Six'),
            )),
        ))
Ejemplo n.º 2
0
class EUExitDomesticContactForm(
        SerializeMixin,
        ZendeskActionMixin,
        ConsentFieldMixin,
        forms.Form,
):

    COMPANY = 'COMPANY'

    COMPANY_CHOICES = (
        (COMPANY, 'Company'),
        ('OTHER', 'Other type of organisation'),
    )

    first_name = forms.CharField()
    last_name = forms.CharField()
    email = forms.EmailField()
    organisation_type = forms.ChoiceField(
        label='Business type',
        widget=forms.RadioSelect(),
        choices=COMPANY_CHOICES,
    )
    company_name = forms.CharField()
    comment = forms.CharField(
        label='Your question',
        help_text="Please don't share any commercially sensitive information.",
        widget=Textarea,
        validators=[
            no_html,
            not_contains_url_or_email,
        ],
    )
    captcha = ReCaptchaField(label='', label_suffix='', widget=ReCaptchaV3())
Ejemplo n.º 3
0
def test_radio_select_widget():
    widget = forms.RadioSelect(attrs={'id': 'radio-test'},
                               choices=TEST_CHOICES)
    html = widget.render('name', 'value')
    soup = BeautifulSoup(html, 'html.parser')

    assert '<label ' in html
    assert '<ul ' in html

    list_element = soup.find('ul')
    assert list_element['id'] == 'radio-test'
Ejemplo n.º 4
0
def test_widget_id_handles_spaces_and_uppercase():
    widget = forms.RadioSelect(use_nice_ids=True,
                               attrs={'id': 'radio-test'},
                               choices=TEST_CHOICES_WITH_SPACES)
    html = widget.render('name', 'value')
    soup = BeautifulSoup(html, 'html.parser')
    exp_ids = ['cyan-colour', 'magenta-colour', 'yellow-colour']

    inputs = soup.find_all('input')
    for input, exp_id in zip(inputs, exp_ids):
        assert input.attrs['id'] == 'radio-test-{}'.format(exp_id)
Ejemplo n.º 5
0
class WhatAreYouSellingForm(forms.Form):
    PRODUCTS = 'PRODUCTS'
    SERVICES = 'SERVICES'
    PRODUCTS_AND_SERVICES = 'PRODUCTS_AND_SERVICES'
    CHOICES = (
        (PRODUCTS, 'Products'),
        (SERVICES, 'Services'),
        (PRODUCTS_AND_SERVICES, 'Products and Services'),
    )
    choice = forms.ChoiceField(
        label='',
        widget=forms.RadioSelect(),
        choices=CHOICES,
    )
Ejemplo n.º 6
0
def test_radio_default_ids():
    widget = forms.RadioSelect(attrs={'id': 'radio-test'},
                               choices=TEST_CHOICES)
    html = widget.render('name', 'value')
    soup = BeautifulSoup(html, 'html.parser')

    list_items = soup.find_all('input')
    exp_ids = [
        'radio-test_0',
        'radio-test_1',
        'radio-test_2',
    ]
    for item, exp_id in zip(list_items, exp_ids):
        assert item.attrs['id'] == exp_id
Ejemplo n.º 7
0
def test_radio_nice_ids():
    widget = forms.RadioSelect(use_nice_ids=True,
                               attrs={'id': 'radio-test'},
                               choices=TEST_CHOICES)
    html = widget.render('name', 'value')
    soup = BeautifulSoup(html, 'html.parser')

    list_items = soup.find_all('input')
    exp_ids = [
        'radio-test-cyan',
        'radio-test-magenta',
        'radio-test-yellow',
    ]
    for item, exp_id in zip(list_items, exp_ids):
        assert item.attrs['id'] == exp_id
Ejemplo n.º 8
0
class DemoFormErrors(PrefixIdMixin, forms.Form):
    error_summary_heading = 'There was a problem submitting the 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)
Ejemplo n.º 9
0
class BaseShortForm(forms.Form):
    comment = forms.CharField(
        label='Please give us as much detail as you can',
        widget=Textarea,
    )
    given_name = forms.CharField(label='First name')
    family_name = forms.CharField(label='Last name')
    email = forms.EmailField()
    company_type = forms.ChoiceField(
        label='Company type',
        label_suffix='',
        widget=forms.RadioSelect(),
        choices=COMPANY_TYPE_CHOICES,
    )
    company_type_other = forms.ChoiceField(
        label='Type of organisation',
        label_suffix='',
        choices=(('', 'Please select'), ) + COMPANY_TYPE_OTHER_CHOICES,
        required=False,
    )
    organisation_name = forms.CharField()
    postcode = forms.CharField()
    captcha = ReCaptchaField(label='', label_suffix='', widget=ReCaptchaV3())
    terms_agreed = forms.BooleanField(label=TERMS_LABEL)
Ejemplo n.º 10
0
def test_radio_select_class_has_attrs():
    radio = forms.RadioSelect(attrs={'id': 'radio-test'})
    assert radio.input_type == 'radio'
    assert radio.css_class_name == 'g-select-multiple'
    assert radio.attrs['id'] == 'radio-test'
Ejemplo n.º 11
0
class MarketAccessAboutForm(forms.Form):
    error_css_class = 'input-field-container has-error'
    CATEGORY_CHOICES = (
        'I’m an exporter or investor, or I want to export or invest',
        'I work for a trade association',
        'Other',
    )

    firstname = forms.CharField(
        label='First name',
        error_messages={'required': 'Enter your first name'},
    )

    lastname = forms.CharField(
        label='Last name',
        error_messages={'required': 'Enter your last name'},
    )

    jobtitle = forms.CharField(
        label='Job title',
        error_messages={'required': 'Enter your job title'},
    )

    business_type = forms.ChoiceField(
        label='Business type',
        widget=forms.RadioSelect(
            attrs={'id': 'checkbox-single'},
            use_nice_ids=True,
        ),
        choices=((choice, choice) for choice in CATEGORY_CHOICES),
        error_messages={'required': 'Tell us your business type'},
    )
    other_business_type = forms.CharField(
        label='Tell us about your organisation',
        widget=TextInput(attrs={'class': 'js-field-other'}),
        required=False,
    )

    company_name = forms.CharField(
        label='Business or organisation name',
        error_messages={
            'required': 'Enter your business or organisation name'
        },
    )

    email = forms.EmailField(
        label='Email address',
        error_messages={'required': 'Enter your email address'},
    )

    phone = forms.CharField(
        label='Telephone number',
        error_messages={'required': 'Enter your telephone number'},
    )

    def clean(self):
        data = self.cleaned_data
        other_business_type = data.get('other_business_type')
        business_type = data.get('business_type')
        if business_type == 'Other' and not other_business_type:
            self.add_error('other_business_type', 'Enter your organisation')
        else:
            return data