Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document adding a field dynamically #195

Open
asfaltboy opened this issue Jul 2, 2018 · 0 comments
Open

Document adding a field dynamically #195

asfaltboy opened this issue Jul 2, 2018 · 0 comments
Assignees

Comments

@asfaltboy
Copy link

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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants