questions.form module

class questions.form.Form(name='', action='', html_id='questions_form', theme='defaultV2', platform='jquery', resource_url=None, **params)[source]

Bases: object

This is the base class used for creating user-defined forms. In addition to setting up the form configuration and performing validation, it generates the SurveyJS form JSON and keeps track of the required Javascript and CSS resources.

Parameters:
  • name (str) – The name of the form. If empty, the class name is used.

  • action (str) – The URL where the form data will be posted. If empty, the same URL for the form is used.

  • html_id (str) – The id for the div element that will be used to render the form.

  • theme (Literal['defaultV2', 'bootstrap', 'modern']) – The name of the base theme for the form. Default value is ‘defaultV2’.

  • platform (Literal['angular', 'jquery', 'knockout', 'react', 'vue']) – The JS platform to use for generating the form. Default value is ‘jquery’.

  • resource_url (Optional[str]) – The base URL for the theme resources. If provided, Questions will expect to find all resources under this URL. If empty, the SurveyJS CDN will be used for all resources.

  • params – Optional list of parameters to be passed to the SurveyJS form object.

property css

Combined CSS resources for this form.

default_params = {}
property extra_css

Any extra CSS resources required by the form’s question types.

property extra_js

Any extra JS resources required by the form’s question types.

classmethod from_json(form_json, name)[source]

Generate a Form class definition from properly formatted JSON data. The The generated form class can then be instantiated as needed.

Parameters:
  • form_json (str) – A well formed JSON string with a SurveyJS definition.

  • name (str) – The name of the generated class.

Returns:

A new Python Type that is a subclass of Form.

property js

Combined JS resources for this form.

questions_resource_url = 'https://unpkg.com'
render_html(title=None, form_data=None)[source]

Render a full HTML page showing this form.

Parameters:
  • title (Optional[str]) – The form title.

  • form_data (Optional[Dict[str, Any]]) – answers to show on the form for each question (for edit forms).

Returns:

String with the generated HTML.

render_js(form_data=None)[source]

Generate the SurveyJS initialization code for the chosen platform.

Parameters:

form_data (Optional[Dict[str, Any]]) – answers to show on the form for each question (for edit forms).

Returns:

String with the generated javascript.

property required_css

Required CSS resources needed to run SurveyJS on chosen platform.

property required_js

Required JS resources needed to run SurveyJS on chosen platform.

classmethod set_resource_url(url)[source]
to_json()[source]

Convert the form to JSON, in the SurveyJS format.

Returns:

JSON object with the form definition.

update_object(obj, form_data)[source]

Utility method to set an object’s attributes with data obtained from a form. This method validates the data before setting the object’s attributes.

Parameters:
  • obj (Any) – The object to set attributes on.

  • form_data (Dict[str, Any]) – A dictionary-like object with the form data to be validated.

Raises:

questions.validators.ValidationError if validation does not pass.

validate(form_data, set_errors=False)[source]

Server side validation mimics what client side validation should do. This means that any validation errors here are due to form data being sent from outside the SurveyJS form, possibly by directly posting the data to the form. Questions keeps track of the errors, even though the UI will show them anyway. Validation returns False if at least one validator doesn’t pass.

Parameters:
  • form_data (Dict[str, Any]) – A dictionary-like object with the form data to be validated.

  • set_errors (bool) – set to True to add an __errors__ key to the form data dictionary, containing the validation errors.

Returns:

True if the validation passes, False otherwise.

class questions.form.FormPage(form, name='', **params)[source]

Bases: object

Represents an individual page from a multi-page form.

Parameters:
  • form (Type[Form]) – A subclass of questions.Form (not an instance). The form to be shown in its own page.

  • name (str) – The name of the form.

  • params – Optional list of parameters to be passed to the SurveyJS page object.

class questions.form.FormPanel(form, name='', dynamic=False, **params)[source]

Bases: object

A panel is a set of fields that go together. It can be used for visual separation, or as a dynamically added group of fields for complex questions.

Parameters:
  • form (Type[Form]) – A subclass of questions.Form (not an instance). The form to be shown in its own page.

  • name (str) – The name of the form.

  • dynamic (bool) –

    Set to True if the panel will be used as a template for adding

    or removing groups of questions.

  • params – Optional list of parameters to be passed to the SurveyJS panel object.

questions.form.to_camel_case(name)[source]