durusmail: quixote-users: question on OptionSelectWidget
question on OptionSelectWidget
2005-06-16
2005-06-16
question on OptionSelectWidget
Roger E. Masse
2005-06-16
Sells, Fred writes:

 > Could someone provide a little snippet that shows how to handle the post
 > resulting from the OptionSelectWidget action?

Just a personal preference but I would not do it as a widget first
but rather as a form.  When you've got it working, convert to a
CompositeWidget if that still makes sense.

                -Roger

Here's the example I was thinking of to approach your problem...
I've not tried to execute this, so watch for bugs.

from quixote import redirect
from quixote.form import Form
from quixote.form.widget import OptionSelectWidget
from quixote.form.widget import SubmitWidget, TextWidget

def yesno_explain_form():

    form = Form()
    form.add(OptionSelectWidget(name='yesno', title = '',value=False,
                                options=[(True, 'Yes'), (False, 'No')])
    form.add(SubmitWidget, 'cancel', 'Cancel')
    form.add(SubmitWidget, 'submit', 'Submit')

    if form.get('cancel'):
       return redirect('..')

    def render ():
        title = 'yes no explain'
        s = header(title)
        s += form.render()
        s += footer(title)
        return s

    if not form.is_submitted() or form.has_errors():
       return render()

    if form.get_submit() == True: # submitted by OptionSelect
       form.add(TextWidget, 'explain', 'Explanation')
       form.clear_errors()
       return render()

    if not form.get('yesno') and not form.get('explain'):
       form.set_error('explain', 'Please explain why not')
    if form.has_errors():
       return render()

    assert form.get('submit')

    foo = form.get('yesno')
    explain = form.get('explain')
    # Save values somehow

    return redirect('..')

reply