ok i tried StringBool.. same problem..
the problem:
if a tick bock is un ticked there is no checkbox-name in the return
result
hence in either StringBool or Bool there is no result sent back
it is then not in the dict of response if the user un ticked the box.
so upon a validation error the params given to the controler method
have no idea what the value is and puts back the default
but you hit a nail...
strategically place a <checkbox> followed by a <hidden Input > of the
same name in the html.
now if the tick box is present and ticked: True (the value) is sent
back
if the tick box is unticked it doesn't return .. instead the hidden
box of the same name returns (which set to false)
html:
<span>
<input type="checkbox" name="${name}" class="${css_class}" id="$
{id}"
value="True"
py:attrs="attrs" checked="${value}" />
<input type="hidden" name="${name}" id="$id" value="False" />
</span>
with this the return result always has a value in it.. and stringbool
used
class CheckBox(OldCheckBox):
validator = StringBool
template = the above html
i just hope browsers remain consistent. a better approach would be
for the current CheckBox to just poke in the validated response. to
the return parameters in the tmp_l.context.form_values
On Mar 14, 6:44 am, "Diez B. Roggisch" <[email protected]> wrote:
> 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
> > athttp://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.