Package aquarium :: Package util :: Module FormValid :: Class Field
[show private | hide private]
[frames | no frames]

Class Field


This class does the majority of the work. A field has many keyword args that can be set at instantiation that define how the field is validated. In addition there are two required parameters.

Required parameters:

name
The name of the field. Must be the same as the field name that is used as the index in the values dict that is passed in to validate against.
errorMsg
The error message to raise as part of the exception on failure.

Optional parameters:

cast
A function callback that will be used to attempt to cast a value before attempting to validate it.
default
If given, this is the default value to use for validation when the user did not specify one. To give the effect of an optional field in your form, set the default to "".
function
A function callback that will be used to validate this value. Expected to return true on success and false on failure. Function should accept one argument, the value.
regex
A compiled regex object (i.e. the result of a re.compile()) that will be used to validate the value against using re.match()
dependents
A list of tuples of the form (regex, Form). If the value of this field matches the given regex we will attempt validation of Form. Otherwise, Form is ignored. Since Form is ignored any data the user has entered into those fields will not be included in the results dict and will thus require extra work to send them back to the user.
strip
A boolean that defaults to True stating whether or not to .strip() a string before attempting validation. Will catch AttributeError and pass if you tried to strip() a non-string.

Method Summary
  __init__(self, name, errorMsg, **kargs)
  __repr__(self)
  validate(self, values)
Attempts to validate this field.

Method Details

validate(self, values)

Attempts to validate this field. Because of dependencies it is required that "values" be passed in as a dict object containing, at a minimum, access to values for all Fields in this Field and its dependencies.

As a postcondition, self.result is a dictionary containing all of the Fields we validated (valid or invalid does not matter). The dict is indexed by field name and contiains a Field object. The value property represents the value after casting (if applicable). Other properties include the errorMsg, and whether or not the field is invalid.

Dependencies of a field, if validated, are returned as part of this dictionary. There is no nesting, so the multi-level paradigm used to enter a form is lost on return; the results are flattened.


Generated by Epydoc 2.1 on Mon Jan 1 16:34:19 2007 http://epydoc.sf.net