It doens't work, and it cannot works !
I tried this
class FormForwarding(widgets.TableForm):
fields = [
MultipleHiddenFields('context'),
widgets.CheckBox('active', label="Activate forwarding",
validator=validators.StringBool(if_empty=False)),
widgets.TextField('forward_addr', label="Address to forward
to", attrs=dict(size=40, maxlength=255), validator=EmailValidator()),
widgets.CheckBox('keep', label="Keep a copy on server",
validator=validators.StringBool(if_empty=False, if_invalid=False )),
]
submit_text = "Update forwarding"
And added some debuging into StringBool
def _to_python(self, value, state):
print "================================================="
if isinstance(value, (str, unicode)):
value = value.strip().lower()
if value in self.true_values:
return True
if not value or value in self.false_values:
return False
raise Invalid(self.message("string", state,
true=self.true_values[0],
false=self.false_values[0]),
value, state)
return bool(value)
def _from_python(self, value, state):
print "================================================="
if value:
return self.true_values[0]
else:
return self.false_values[0]
When both are checked, I can see the 4 '====' line
But when one is missing, non of them.
This is because the call (that generate the error) is done before the
call of the validator !
I thins this is because one decorator is taking the signature of the
function.
And then raise an exception because of missing argument.
Their is no solution with TG 1.x today for missing value !
It should be in red flashing in the wiki somewhere !
Regards
On Jun 20, 5:52 am, "Ian Wilson" <[EMAIL PROTECTED]> wrote:
> You probably want StringBool for one thing(it gets me all the time).
> StringBool: 'True'==True, 'False' == False, '' == None
> Bool: 'any string' == 'True' == 'False' == True, '' == false
>
> Also you can use the following to handle cases when they are not sent
> at all or are invalid:
> if_empty=False, if_invalid=False
>
> So in the end you probably just want this for both the keep and active fields:
> validator=validator.StringBool(if_empty=False)
>
> Your error is most likely being caused by the missing of if_empty=False.
>
> Not using StringBool will cause problems later(or maybe as soon as you
> add if_empty and try it again).
>
> You can test them on the console using:
> import turbogears.validators as validators
> validators.StringBool.to_python(value)
> validators.Bool.to_python(value)
>
> -Ian
>
> On 6/19/07, aspineux <[EMAIL PROTECTED]> wrote:
>
>
>
> > Here is y code
>
> > class FormForwarding(widgets.TableForm):
> > fields = [
> > MultipleHiddenFields('context'),
> > widgets.CheckBox('active', label="Activate forwarding",
> > validator=validators.Bool()),
> > widgets.TextField('forward_addr', label="Address to forward
> > to", attrs=dict(size=40, maxlength=255), validator=EmailValidator()),
> > widgets.CheckBox('keep', label="Keep a copy on server",
> > validator=validators.Bool()),
> > ]
> > submit_text = "Update forwarding"
>
> > here is my "action" method :
>
> > @turbogears.expose()
> > @validate(form=forwarding_form)
> > @error_handler(index)
> > def update_forwarding(self, email_addr, forward_addr, active,
> > keep, **kwargs):
> > ...
>
> > If one of both checkbox is unchecked, I get error:
>
> > exceptions.TypeError: ('update_forwarding() takes at least 5 non-
> > keyword arguments (3 given)', <function _wrapper at 0xa5e5224>)
>
> > more debug info
> > args ()
> > errors []
> > fn <bound method Mailbox.update_forwarding of
> > <emailgency.addr_ctrl.Mailbox object at 0xa39cb6c&g...t;>
> > kw {'email_addr': u'[EMAIL PROTECTED]', 'forward_addr':
> > u'[EMAIL PROTECTED]', 'keep': u'on...'}
> > predicate <turbogears.identity.conditions.in_any_group object at
> > 0xa2e8a0c>
>
> > << errors= []
> > if predicate.eval_with_object( current, errors ):
> > return fn( *args, **kw )
> > else:
> > raise IdentityFailure( errors )>> return fn( *args,
> > **kw )
> > exceptions.TypeError: ('update_forwarding() takes at least 5 non-
> > keyword arguments (3 given)', <function _wrapper at 0xa5e5224>)
>
> > TG 1.0.2.2
>
> > Regards
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---