About Formish

Formish is a templating language agnostic form generation and handling library.

How do I try it out

Creating a schema

First of all we need to create a data schema to define what types of data we want in the form. Lets take a look at the structure of a Form instance to begin with

>>> import schemaish
>>> schema = schemaish.Structure()
>>> schema.add( 'myfield', schemaish.String() )
>>> schema.attrs
[('myfield', <schemaish.attr.String object at 0x...>)]

So we now have a single field in our schema which is defined as a string. We can now create a form from this

>>> import formish
>>> form = formish.Form(schema)

And what can we do with the form? Well at the moment we have a form name and some fields

>>> form.name
'formish'
>>> for field in form.fields:
...     print field
...
<formish.forms.Field object at 0x...>

And what about our field? Well it’s now become a form field, which means it has a few extra attributes to do with creating things like classes, ids, etc.

>>> field = form.fields.next()
>>> field.name
'myfield'

Obviously the name is what we have it.

>>> field.widget
<bound widget name="myfield", widget="Input", type="String">
>>> field.title
'Myfield'

The title, if not specified in the schema field, is derived from the form name by converting camel case into capitalised words.

>>> field.cssname
'formish-myfield'

This is the start of the templating stuff.. The cssname is an identifier that can be inserted into forms, used for ids and class names.

Form Class

class formish.forms.Form(structure, name=None, defaults={}, errors={}, action_url=None, renderer=None)

The definition of a form

The Form type is the container for all the information a form needs to render and validate data.

Variable name:The name of the form, used to namespace classes and ids
Variable defaults:
 Property to allow getting and setting of defaults
Variable error:Used to indicate a non field specific error on the form. None if no error

Create a new form instance

Parameters:
  • structure (schemaish.Structure) – Schema Structure attribute to bind to the the form
  • name (str “valid html id”) – Optional form name used to identify multiple forms on the same page
  • defaults (dict) – Default values for the form
  • errors (dict) – Errors to store on the form for redisplay
  • action_url (string “url or path”) – Use if you don’t want the form to post to itself
  • renderer (callable) – Something that returns a form serialization when called
action(request, *args)

Find and call the action callback for the action found in the request

Parameters:
  • request (webob.Request) – request which is used to find the action and also passed through to the callback
  • args – list of arguments Pass through to the callback
add_action(callback, name='submit', label=None)

Add an action callable to the form

Parameters:
  • callback (callable) – A function to call if this action is triggered
  • name (string) – The identifier for this action
  • label (string) – Use this label instead of the form.name for the value of the action (for buttons, the value is used as the text on the button)
fields
Return a generator that yields all of the fields at the top level of the form (e.g. if a field is a subsection or sequence, it will be up to the application to iterate that field’s fields.
validate(raw_request)
Get the data without raising exceptions and then validate the data. If there are errors, raise them; otherwise return the data
__call__()
Calling the Form generates a serialisation using the form’s renderer

Field Class

class formish.forms.Field(name, attr, form)

A wrapper for a schema field type that includes form information.

The Schema Type Atribute does not have any bindings to the form library, it can be used on it’s own. We bind the Schema Attribute to a Field in order to include form related information.

Method __call__:
 

returns a serialisation for this field using the form’s renderer - read only

Parameters:
  • name – Name for the field
  • attr (schemaish.attr.*) – Schema attr to bind to the field
  • form (formish.Form instance.) – The form the field belongs to.
title
The Field schema’s title
description
The Field schema’s description
cssname
cssname identifier for the field
classes
Works out a list of classes that should be applied to the field
value
Convert the request_data to a value object for the form or None.
required
Does this field have a Not Empty validator of some sort
error
Lazily get the error from the form.errors when needed
widget
return the fields widget bound with extra params.
__call__()
returns a serialisation for this field using the form’s renderer