Multiple instances of the same form field

I invite a form with two fields defined as an identity and an email address, as follows:

class InviteForm(Form):
    person = TextField("person", validators=[validators.Required("Please enter persons name.")])
    email =  EmailField("email", validators=[validators.Required("Please enter valid email."), validators.Email("Please enter valid email.")])

    def validate(self):
        return validate_form(self)

Where the validate_form function is a cusotm validator that checks for several conditions for an invitation.

My requirement is to allow users to invite more than one person at a time. To do this, I added a jquery function that replicates these fields in html form and allows you to invite several people.

But the problem is my vision function, when I extract the results from the mail request, it gives information only from the first person. How can I get all the data about faces. My view is defined as follows:

@app.route("/invite", methods=["GET", "POST"])
def invite():
   invite_form = InviteForm()
   if invite_form.validate_on_submit():
       print invite_form.person
       print invite_form.email

This gives only one field, not an array of fields.

Is this possible with python wtf? How?

+4
1

, , FormField, WTF ( ).

: html, . javascript, , person-3.

from flask import Flask, render_template_string
from flask.ext.wtf import Form
from wtforms import FieldList, StringField

app = Flask(__name__)
app.secret_key = 'TEST'


class TestForm(Form):
    person = FieldList(StringField('Person'), min_entries=3, max_entries=10)
    foo = StringField('Test')


@app.route('/', methods=['POST', 'GET'])
def example():
    form = TestForm()
    if form.validate_on_submit():
        print form.person.data
        ## [u'One', u'Two', u'Three']

    return render_template_string(
        """
            <form method="post" action="#">
            {{ form.hidden_tag() }}
            {{ form.person }}
            <input type="submit"/>
            </form>
        """, form=form)


if __name__ == '__main__':
    app.run(debug=True)
+5

All Articles