def on_submission(self, request, **kwargs): data = request.POST.copy() # Add form fields to data if they dont exist (checkboxes/radio buttons will be missing if they're not selected) for component in get_all_form_components(self.form): if component.name not in data: data[component.name] = None return data
def get_next_form_page(self, form_pk, action, request, post_errors): form = copy.deepcopy(next(form for form in self.get_forms().forms if str(form.pk) == form_pk)) # Add form fields to validated_data if they dont exist for component in get_all_form_components(form): if component.name not in self._validated_data and component.name[:-2] not in self._validated_data: self._validated_data[component.name] = "" if action == Actions.SUBMIT or action == Actions.RETURN: validated_data = validate_data_unknown( self.get_object_pk(), self.get_action(), request, nest_data(self.get_validated_data()) ) validated_data["errors"] = validated_data.get("errors", {}) errors = validated_data["errors"] if post_errors: errors.update(post_errors) if errors: errors = flatten_data(validated_data["errors"]) errors = remove_unused_errors(errors, form) if errors: insert_hidden_fields(self.get_validated_data(), form) if action == Actions.RETURN: form = convert_form_to_summary_list_instance(form) return form_page( request, form, data=self.get_validated_data(), errors=errors, extra_data={"form_pk": form.pk, **self.additional_context}, ) if action != Actions.RETURN: next_form = get_next_form(form.pk, self.get_forms()) if next_form: insert_hidden_fields(self.get_validated_data(), next_form) return form_page( request, next_form, data=self.get_validated_data(), extra_data={"form_pk": next_form.pk, **self.additional_context}, ) elif action == Actions.CHANGE: insert_hidden_fields(self.get_validated_data(), form) return form_page( request, convert_form_to_summary_list_instance(form), data=self.get_validated_data(), extra_data={"form_pk": form.pk, **self.additional_context}, ) if self.validate_only_until_final_submission: return self.generate_summary_list() return redirect(request.path)
def test_get_all_form_components(self): form = Form( title="I Am Easy to Find", questions=[ RadioButtons( name="hello", options=[ Option("key", "value", components=[TextInput("text")]), Option("key2", "value", components=[TextInput("text2")]), ], ) ], ) components = get_all_form_components(form) self.assertEqual(len(components), 3)
def __init__( self, title=None, description=None, questions=None, caption=None, buttons=None, helpers=None, footer_label: Label = None, javascript_imports: Optional[Set[str]] = None, default_button_name="Save", default_button_style=ButtonStyle.DEFAULT, back_link=BackLink(), post_url=None, container: str = "two-pane", ): from lite_forms.helpers import convert_to_markdown, heading_used_as_label self.title = title self.description = convert_to_markdown(description) self.questions = questions self.caption = caption self.helpers = helpers self.footer_label = footer_label self.buttons = buttons self.back_link = back_link if self.buttons is None: self.buttons = [ Button(default_button_name, "submit", style=default_button_style) ] self.javascript_imports = javascript_imports or set() self.post_url = post_url self.single_form_element = heading_used_as_label(questions) self.container = container from lite_forms.helpers import get_all_form_components if self.questions: for component in get_all_form_components(self): for item in getattr(component, "javascript_imports", []): self.javascript_imports.add(item)