Skip to content

Instantly share code, notes, and snippets.

@prokoptsev
Last active August 29, 2015 14:23

Revisions

  1. prokoptsev revised this gist Jun 15, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Formsets_in_WTForms.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # from https://groups.google.com/forum/#!topic/wtforms/ur84vD5S66Y
    # source: https://groups.google.com/forum/#!topic/wtforms/ur84vD5S66Y

    # Not exactly, but there are multiple ways to do this in WTForms that
    # are different, each with its own advantages.
  2. prokoptsev renamed this gist Jun 15, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. prokoptsev renamed this gist Jun 15, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. prokoptsev renamed this gist Jun 15, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. prokoptsev created this gist Jun 15, 2015.
    45 changes: 45 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # from https://groups.google.com/forum/#!topic/wtforms/ur84vD5S66Y

    # Not exactly, but there are multiple ways to do this in WTForms that
    # are different, each with its own advantages.

    # Old Method: For two completely different forms which you want to
    # validate entirely separately, you can create two form instances, each
    # with their own prefix. This was the only way prior to WTForms 0.4 or
    # thereabouts, to run multiple forms.


    def login_register_view(request):
    reg_form = RegisterForm(request.POST, prefix='register')
    login_form = LoginForm(request.POST, prefix='login')


    # This creates fields named like register-username, login-username, and
    # the like. You also get fine-grained control over validating each form
    # separately (perhaps, in response to a SubmitField or the like)

    # Now, an even more powerful solution, which provides basically
    # everything that django's FormSet provides, is to use FormField and
    # FieldList. This can be done for a master-detail record validated
    # together, or a master record with multiple sub-records wherein you can
    # control how many sub-lists exist.


    class ArticleEdit(Form):
    # forms containing enclosures may contain bare fields, but aren't
    # required to
    title = TextField()

    # a related child object editing. form.summary['foo'] or
    # form.summary.form.foo can access the fields of ArticleSummaryForm.
    summary = FormField(ArticleSummaryForm)

    # The most powerful enclosure type. This combines FieldList
    # with FormField to create a multiple-edit scenario.
    pages = FieldList(FormField(ArticlePageForm), min_entries=1, max_entries=10)

    # There are some examples of how to use FieldList on the documentation;
    # and with the appropriate view logic, you can create a number of
    # powerful forms, such as multiple-edit-inline with individual
    # validation as required. With all enclosures, validating the outermost
    # enclosure will call validate() on any enclosed fields and forms.