Ejemplo n.º 1
0
    def test_unsaved_fields_in_form_builder_formfields(self):
        """Ensure unsaved FormField instances are added to FormBuilder.formfields dict
        with a clean_name as the key.
        """
        unsaved_field_1 = FormField(
            page=self.form_page,
            sort_order=14,
            label="Unsaved field 1",
            field_type="singleline",
            required=True,
        )
        self.form_page.form_fields.add(unsaved_field_1)

        unsaved_field_2 = FormField(
            page=self.form_page,
            sort_order=15,
            label="Unsaved field 2",
            field_type="singleline",
            required=True,
        )
        self.form_page.form_fields.add(unsaved_field_2)

        fb = FormBuilder(self.form_page.get_form_fields())
        self.assertIn(get_field_clean_name(unsaved_field_1.label), fb.formfields)
        self.assertIn(get_field_clean_name(unsaved_field_2.label), fb.formfields)
Ejemplo n.º 2
0
    def clean(self):

        super().clean()

        # Check for dupe form field labels - fixes #585
        if "form_fields" in self.formsets:
            _forms = self.formsets["form_fields"].forms
            for f in _forms:
                f.is_valid()

            for i, form in enumerate(_forms):
                if "label" in form.changed_data:
                    label = form.cleaned_data.get("label")
                    clean_name = get_field_clean_name(label)
                    for idx, ff in enumerate(_forms):
                        # Exclude self
                        ff_clean_name = get_field_clean_name(
                            ff.cleaned_data.get("label"))
                        if idx != i and clean_name == ff_clean_name:
                            form.add_error(
                                "label",
                                django.forms.ValidationError(
                                    _("There is another field with the label %s, please change one of them."
                                      ) % label),
                            )
Ejemplo n.º 3
0
    def get_field_clean_name(self):
        """
        Prepare an ascii safe lower_snake_case variant of the field name to use as the field key.
        This key is used to reference the field responses in the JSON store and as the field name in forms.
        Called for new field creation, validation of duplicate labels and form previews.
        When called, does not have access to the Page, nor its own id as the record is not yet created.
        """

        return get_field_clean_name(self.label)
Ejemplo n.º 4
0
    def save(self, *args, **kwargs):
        """
        When new fields are created, generate a template safe ascii name to use as the
        JSON storage reference for this field. Previously created fields will be updated
        to use the legacy unidecode method via checks & _migrate_legacy_clean_name.
        """

        is_new = self.pk is None
        if is_new:
            clean_name = get_field_clean_name(self.label)
            self.clean_name = clean_name

        super().save(*args, **kwargs)
Ejemplo n.º 5
0
    def formfields(self):
        formfields = OrderedDict()

        for field in self.fields:
            options = self.get_field_options(field)
            create_field = self.get_create_field_function(field.field_type)

            # If the field hasn't been saved to the database yet (e.g. we are previewing
            # a FormPage with unsaved changes) it won't have a clean_name as this is
            # set in FormField.save.
            clean_name = field.clean_name or get_field_clean_name(field.label)
            formfields[clean_name] = create_field(field, options)

        return formfields
Ejemplo n.º 6
0
    def save(self, *args, **kwargs):
        """
        When new fields are created, generate a template safe ascii name to use as the
        JSON storage reference for this field. Previously created fields will be updated
        to use the legacy unidecode method via checks & _migrate_legacy_clean_name.
        We do not want to update the clean name on any subsequent changes to the label
        as this would invalidate any previously submitted data.
        """

        is_new = self.pk is None
        if is_new:
            clean_name = get_field_clean_name(self.label)
            self.clean_name = clean_name

        super().save(*args, **kwargs)