Use the Validator StringBool.

Diez

Am 12.03.2010 um 06:05 schrieb steve <[email protected]>:

the problem:
   on a form validation error a checked box (Bool) loses its state on
the form refresh

setup:
 a form created with a tw.form.TableForm, TextField, CheckBox
 a formencode.validator Bool and i used Email

the widget:
class form_w(TableForm):
    fields = [
      TextField ('email', is_required=True, label_text="Email",
          validator=Email()),
      CheckBox('want_info', label_text='Want_info',
          default=True,
          validator=Bool()
      ]

Email_form = form_w('Email_form', action="email_capture",
submit_text='submit')

the Controller:
@expose(pws.template.index)
def index(self, **kws):
   tmpl_context.form=Email_form
   return dict(page='index', data=kws)

@expose(pws.template.index)
def index_validate(self, **kws):
   tmpl_context.form = Email_form
   return dict(page='index', data=kws)

@validate(Email_form, error_handler=index_validate)
@expose(pws.template.index)
def email_capture(self, **kws):
   flash("capture the data %s" %kws)
   tmpl_context.form=Email_form
   return dict(page='index', data=kws)

1)when the form is initialized the check box is ticked.
2)i put in the email address incorrectly, untick the box,  submit it.
3) i get the email error and the check box is ticked again.

if i get the email correct the check box keeps it state correctly

the tmpl_context.form_values have the same info as the kws

to make it work correctly in the def index_validate i add
  if not kws.has_key('want_info'):
      kws['want_info'] = False

the formencode does know this as it has a if_missing=False which is
returned should the validation be sucessful.. it is not added to the
return result when missing because the browser didn't send back the
information.

i think this is a bug. as the validation on the box was correct it
should have poked in the missing dict key=False in the
tmpl_context.form_values as they represent the python side which
1)then are used to be sent back out on the validation_form.
2)when officially passed validation is returned with the correct False
entity

should the validation be sucessful even the kws has a
kws['want_info']=False in it. the problem is only when the form does
not validate.

-- You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected] . For more options, visit this group at http://groups.google.com/group/turbogears?hl=en .


--
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en.

Reply via email to