Package gluon :: Module html :: Class FORM
[hide private]
[frames] | no frames]

Class FORM

source code

  object --+        
           |        
XmlComponent --+    
               |    
             DIV --+
                   |
                  FORM
Known Subclasses:

Examples:

>>> from validators import IS_NOT_EMPTY
>>> form=FORM(INPUT(_name="test", requires=IS_NOT_EMPTY()))
>>> form.xml()
'<form action="#" enctype="multipart/form-data" method="post"><input name="test" type="text" /></form>'

a FORM is container for INPUT, TEXTAREA, SELECT and other helpers

form has one important method:

   form.accepts(request.vars, session)

if form is accepted (and all validators pass) form.vars contains the accepted vars, otherwise form.errors contains the errors. in case of errors the form is modified to present the errors to the user.

Instance Methods [hide private]
 
__init__(self, *components, **attributes)
Args: components: any components that should be nested in this element attributes: any attributes you want to give to this element
source code
 
_postprocessing(self)
Handling of attributes (normally the ones not prefixed with '_').
source code
 
accepts(self, request_vars, session=None, formname='default', keepvalues=False, onvalidation=None, hideerror=False, **kwargs)
kwargs is not used but allows to specify the same interface for FORM and SQLFORM
source code
 
add_button(self, value, url, _class=None) source code
 
as_dict(self, flat=False, sanitize=True)
EXPERIMENTAL
source code
 
as_json(self, sanitize=True) source code
 
as_xml(self, sanitize=True) source code
 
as_yaml(self, sanitize=True) source code
 
assert_status(self, status, request_vars) source code
 
hidden_fields(self) source code
 
process(self, **kwargs)
Perform the .validate() method but returns the form
source code
 
validate(self, **kwargs)
This function validates the form, you can use it instead of directly form.accepts.
source code
 
xml(self)
generates the xml for this component.
source code

Inherited from DIV: __delitem__, __getitem__, __len__, __nonzero__, __setitem__, __str__, append, element, elements, flatten, insert, sibling, siblings, update

Inherited from DIV (private): _fixup, _setnode, _traverse, _validate, _wrap_components, _xml

Inherited from XmlComponent: __add__, __mul__, add_class, remove_class

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Static Methods [hide private]
 
confirm(text='OK', buttons=None, hidden=None) source code
Class Variables [hide private]
  REDIRECT_JS = 'window.location=\'%s\';return false'
  tag = 'form'

Inherited from DIV: regex_attr, regex_class, regex_id, regex_tag

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, *components, **attributes)
(Constructor)

source code 

Args:
    components: any components that should be nested in this element
    attributes: any attributes you want to give to this element

Raises:
    SyntaxError: when a stand alone tag receives components

Overrides: object.__init__
(inherited documentation)

_postprocessing(self)

source code 

Handling of attributes (normally the ones not prefixed with '_').

Nothing to postprocess yet. May be overridden by subclasses

Overrides: DIV._postprocessing
(inherited documentation)

as_dict(self, flat=False, sanitize=True)

source code 

EXPERIMENTAL

Sanitize is naive. It should catch any unsafe value for client retrieval.

process(self, **kwargs)

source code 

Perform the .validate() method but returns the form

Usage in controllers:

   # directly on return
   def action():
       #some code here
       return dict(form=FORM(...).process(...))

You can use it with FORM, SQLFORM or FORM based plugins:

   # response.flash messages
   def action():
       form = SQLFORM(db.table).process(message_onsuccess='Sucess!')
       return dict(form=form)

   # callback function
   # callback receives True or False as first arg, and a list of args.
   def my_callback(status, msg):
       response.flash = "Success! "+msg if status else "Errors occured"

   # after argument can be 'flash' to response.flash messages
   # or a function name to use as callback or None to do nothing.
   def action():
       return dict(form=SQLFORM(db.table).process(onsuccess=my_callback)

validate(self, **kwargs)

source code 

This function validates the form,
you can use it instead of directly form.accepts.

Usage:
In controller::

    def action():
        form=FORM(INPUT(_name="test", requires=IS_NOT_EMPTY()))
        form.validate() #you can pass some args here - see below
        return dict(form=form)

This can receive a bunch of arguments

onsuccess = 'flash' - will show message_onsuccess in response.flash
            None - will do nothing
            can be a function (lambda form: pass)
onfailure = 'flash' - will show message_onfailure in response.flash
            None - will do nothing
            can be a function (lambda form: pass)
onchange = 'flash' - will show message_onchange in response.flash
            None - will do nothing
            can be a function (lambda form: pass)

message_onsuccess
message_onfailure
message_onchange
next      = where to redirect in case of success
any other kwargs will be passed for form.accepts(...)

xml(self)

source code 

generates the xml for this component.

Overrides: XmlComponent.xml