Pylons / webtest

Wraps any WSGI application and makes it easy to send test requests to that application, without starting up an HTTP server.
https://docs.pylonsproject.org/projects/webtest/en/latest/
Other
336 stars 110 forks source link

Document adding a field dynamically #195

Open asfaltboy opened 6 years ago

asfaltboy commented 6 years ago

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')