durusmail: qp: Re: Form reset?
Form reset?
2007-10-18
2007-10-29
2007-10-29
2007-10-29
2007-10-29
2007-10-30
2007-10-30
2007-10-30
2007-10-30
2007-10-30
Re: Form reset?
2007-10-30
2007-10-30
2007-10-30
2007-11-02
2007-11-19
Re: Form reset?
Michael Watkins
2007-10-30
* David K. Hess wrote on [2007-10-30 11:31:41 -0500]:
> I'm not sure how I would apply that outside of an AJAX setting? I'm trying
> to reset the values of the form after it has been submitted but before it
> has been rendered for return to the client.

A HTML 'reset' resets the form purely on the client side; there is
no POST to the server. While its handy and useful:

    form.add_reset('reset', 'Reset')

.... its probably not what you want.

If I read you correctly, you:

- have more than one form on a page
- take some action based on the completion of one of the forms (lets
  call it form A) perhaps modifying what should be presented in the
  Response sent to the client after that form is POSTed.
- Want to reset form A to blank or default values after it is
  POSTed.
- Presumably you ignore form A if form B was submitted

This reminds me a little of OptionSelectWidget by the way.

You could (almost) get there by doing something like:

def edit [html] ():

    form_a = form_a_form(default, value, here)
    form_b = form_b_form(some, other, values)

    if not form_b.is_submitted() or form_b.has_errors():
        if form_a.is_submitted():
            do_something_with_values(form_a)
            form_a = form_a_form(default, value, here)
        return  page('Edit',
                     form_a.render(),
                     form_b.render())

I say (almost) because Form.is_submitted() doesn't discriminate
between forms - it processes all the fields in a Request and sets
all to '' if they are not present in the Request. Your form_a gets
processed correctly, but form_b is set to blank.

Perhaps Form should include a form "id" prefix which all widgets
construct field names unique to the form which defines them; then
processing one form on a page won't clobber the balance.

There may be other issues here - I haven't looked very closely at
this.

As a side thought, you could, I suppose, work around this for what I
perceive is your specific use case by checking the request for a
specific submit field name, *prior* to any call to
form_(a|b).is_submitted() ...

def edit [html] (form_option=None):
    form_a = form_a_form(default, value, here)
    form_b = form_b_form(some, other, values, option=form_option)
    # or you could display different forms...
    # if form_option == 'long':
    #    form_b = form_b_long_form(some, other, values)

    # simulate is_submitted for form2
    option = get_request().fields.get('form_option', None)
    if option:
        # form_a was submitted
        get_request().fields.clear() # prevent recursion
        return self.edit(form_option=option)

    # then the reset is the usual straightforward handling
    if not form_b.is_submitted() or form_b.has_errors():
        return  page('Edit',
                     form_a.render(),
                     form_b.render())

Indeed, that works.

Again, this smells a little like OptionSelectWidget; if displaying
different forms or data rested on the value of a single widget,
that's worth looking into.


reply