On Sun, Oct 7, 2012 at 6:24 AM, Mark Huang <
zheng...@gmail.com> wrote:
> Ok I have another question....
>
> Do all the form packages like formalchemy or Deform assume that forms are
> submitted via a "form submit" action?
>
> I am contemplating on using Deform for my form validations, but I am
> currently submitting the forms using an AJAX POST request; sending data as
> JSON to the backend Pyramid. Why? Well, the forms are a bit complex and
> because I use CouchDB, the JSON is easily saved. The problem is, I need to
> write a bunch of code to validate the received JSON from the frontend.
Deform does not care.
Here's the connected pieces:
deform.Form
- is made up of deform.Field objects
- each of which is associated with a
- colander.SchemaNode
- deform.Widget
The form POST data is actually pre-processed by peppercorn, but you
probably don't need to think much about that. The key steps for
validation are the deserialization fo the POST data according to the
Widget hierarchy and then the validation of the resulting cstruct
against the schema hierarchy.
The comments and code for the `deform.Field.validate` method make much
of this clear.
If you're passing in JSON that matches your colander Schema, then you
already have the `cstruct` and just need to call `schema.deserialize`
on it.
The widgets are there to handle the templating for the fields and to
cover other UI details which are orthogonal to the schema model (for
example, a CheckedPasswordWidget serializes to two fields, with a
validator to ensure they match, but deserializes to a single field in
the resulting object... your user only has one password, it's merely a
detail of the form that there are two fields).
This separation of concerns between the widget and the schema is the
reason I like deform so much.
Read the sections called "Deform Components" and "Serialization and
Deserialization" in the deform documentation and things should become
more clear.
http://docs.pylonsproject.org/projects/deform/en/latest/index.html
-Randall