Re: [FE-discuss] Using validators

Top Page
Author: Ian Bicking
Date:  
To: Mike Orr
CC: formencode-discuss
Subject: Re: [FE-discuss] Using validators
Mike Orr wrote:
> I've got a few questions about validators. The FormEncode docs are
> great for explaining how the validators work, but they don't say which
> ones are best for various input controls. I've also got a couple
> complex situations.
>
> 1) What's the right validator for a checkbox? I'm using
> StringBoolean(), and it says "Missing value" unless the box is
> checked. Which is great if the box is labelled "Transfer $1,000,000
> into Mike's bank account", but not for other things.


They should have if_missing=False added.

> 2) Submit buttons should be String? And hidden values too?


Generally I don't name my submit buttons. If you have ignore/remove
hidden set on the schema, they can be nothing (and the value is just
removed). Otherwise string.

> 3) I notice String returns unicode, at least in Pylons with the form
> set to utf-8. Hopefully it will remain that way.


Yes. Perhaps confusingly Unicode does encoding/decoding (though it
should be safe given unicode input, but it would still produce str output).

> 4) My forms have a second submit button labeled Cancel, which simply
> does a redirect to an appropriate place. I could make this a
> hyperlink but would like to keep it a button. I need to bypass all
> the other validators if Cancel is pressed because their value doesn't
> matter. How would I set this up?


You'd have to check this up-front before doing the validation. You
could do:

<button onclick="location.href='location'; return false">Cancel</button>

The degradation isn't great, though. I think if you add a name
attribute you could avoid the submit on cancel, but without Javascript
still test the submission.

> 5) I need to make an attachment widget that's simply an upload control
> if there's no existing attachment. But if there is an existing
> attachment, it needs to show its info and thumbnail, radio buttons for
> "keep", "replace with...", "delete", and an upload control for
> replacement. In Quixote I used a composite widget that returned a
> tuple. How would I make a validator for this? How would I set up the
> dict-dot feature for:
>
> fieldname.file => file upload control
> fieldname.action => radiobutton value ("keep", "replace",
> "delete"), or hidden value ("add")


A composite widget is a schema. As long as you name them these names
and use variabledecode, then "fieldname" will be a dictionary. So you'd
have:

class MyForm(Schema):
     class fieldname(Schema):
         file = File()
         action = String()

You'll get back {'fieldname': {'file': value, 'action': value}}. If you
want to make something reusable you can create all this in a subclass of
Schema, overriding _to_python and _from_python. Some of the
file-related validators in FormEncode are meant to be used much like
this. I could go into more detail later if you want, but can't at the
moment.

   Ian

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss@???
https://lists.sourceforge.net/lists/listinfo/formencode-discuss