On Sep 7, 2006, at 2:06 AM, BJörn Lindqvist wrote:

>
> Thanks to everyone who has explained! I'm very grateful. Unfortunately
> it takes time for me to digest it all, so I only start with the
> validation stuff.
>
> On 9/6/06, Alberto Valverde <[EMAIL PROTECTED]> wrote:
>>> The other reason is that I don't want to use TurboGears widgets, and
>>> @validate and @error_handler seem to be tightly coupled to the  
>>> widget
>>> system.
>>
>> Wrong again. "validate" is tightly coupled with FormEncode *only*.
>> You can skip widgets altogether and use plain FE validators with it:
>> @validate(validator={'param1':Int, 'param2', WhatEver})
>> or
>> @validate(validator=Schema(....))
>
> Ok, understood. But, IMHO, the parameter name "form" in the validate()
> decorator gives the impression that there is a relationship to some
> kind of "form"... I assume you meant to name the keyword parameter
> "validators" instead of "validate"?

The keyword parameter is "validators", sorry. I was referring to the  
decorator when saying "validate".

>
>> If you look at validate's source, you can even notice that form
>> doesn't have to be a widget at all. You can pass any object that has
>> a "validate" method and raises FE's Invalids on wrong input.
>
> I've tried just that. Here is controllers.py from
> http://trac.turbogears.org/turbogears/wiki/SimpleWidgetForm rewritten
> to work without widgets:

> [....]

> I also changed the add.kid template to match Root's add() method. It
> renders the four parameters manually instead of using the widgets
> display() method. When I run the code and submit the form, I always
> get the error "The input field 'self' was not expected"

It's not a bug really but a consequence of the mangling (util.to_kw  
and util.from_kw) "validate" does to the parameters CP injects on  
your controller method so errorhandling can work properly. There's  
not much really you can do about it if using the "validate" decorator.

However, TG provides a formencode.Schema subclass at  
turbogears.validators which you can use to overcome this issue.  
Basically you need to set the "allow_extra_fields" and  
"filter_extra_fields" attributes to a FE plain Schema so it doesn't  
bark and it doesn't let that spurious variables through. This should  
be enough if you don't want to subclass turbogears.validators.Schema  
(it's a "widgetized" Schema ;) )

class MyBaseSchema(formencode.schema.Schema):
        allow_extra_fields = True
        filter_extra_fields = True

I strongly recommend filtering extra fields or else bad things could  
happen... (spooky! ;) ).

Alberto
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to