On Tue, Nov 9, 2010 at 4:51 AM, Ghostwriter <[email protected]> wrote:
> Hi, Everyone!
>
> Some parts of validation-related docs are unclear for me:
> http://pylonshq.com/docs/en/1.0/forms/#validation-the-long-way
> Here we instantiate "schema = EmailForm()" for each request. Various
> code examples in The Pylons Book also follows this practice.
>
> The schema doesn't modify any shared object, does it? So why should we
> do this?
> The same questions applies to other FormEncode primitives - single
> validators, compound validators (ForEach()) and chained validators.
>
> The official documentation says (http://formencode.org/
> Validator.html#other-validator-usage):
> "Validators use instance variables to store their customization
> information. You can use either subclassing or normal instantiation to
> set these...
> ...You can actually use classes most places where you could use an
> instance; .to_python() and .from_python() will create instances as
> necessary, and many other methods are available on both the instance
> and the class level."
>
> As I understand this part, we can create an instance of a schema (or
> any other validation primitive) in the global scope of process and
> then use it in any thread for form validation, right?
>
> So, I'm a little confused here... please clarify this use case.
You can instantiate a single schema for all requests. The standard
@validate usage does this: the instance
is a decorator arg, so it is used by all requests that match that
action. The "customization information" refers to validator options;
e.g., if_empty=0, not_empty=True, accept_python=True, messages={...} ,
not per-request state. So .to_python() and .from_python() are
reentrant.
"You can use either subclassing or normal instantiation to set these".
This is a little obscure. Validator options are class/instance
attributes. The formencode.declarative base class assigns keyword args
to instance attributes, which override the default validator options.
A special class attribute defines how to interpret positional args:
only some validators allow positional args.
--
Mike Orr <[email protected]>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" 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/pylons-discuss?hl=en.