Skip to content

Document adding a field dynamically #195

@asfaltboy

Description

@asfaltboy

In this use case, we are implementing a checkbox/chevron kind of widget, for this we're adding new fields dynamically (with javascript) if they don't yet exist.

While testing this field, we had to add the checkboxes manually in order to be able to submit it. However, we have not found any mention of this in the docs; did we miss it, or is it not documented?

A bit of googling brought us to this SO answer (quoted below), and it seemed to work well for us, except we also had to add the pos argument. Is this the suggested way to do this? Would you appreciate a PR to document this use case?

from webtest.forms import Checkbox

def add_dynamic_checkbox_field(form, name, value):
    """ Add an extra checkbox field to a form. """
    field = Checkbox(form, 'input', None, 1, value)
    form.fields[name] = [field]
    form.field_order.append((name, field))

# usage:
add_dynamic_checkbox_field(resp.form, 'new_field_name', 'some_value')

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions