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.
  _cast(self)
Attempt to cast the data if we're supposed to
  _strip(self)
Strips value, if we're supposed to as defined by the strip karg.
  _validate_dependencies(self, values)
Validates dependencies, if any.
  _validate_function(self)
Call the function callback with self.value if it is defined.
  _validate_regex(self)
Attempt to match against the regex object, if it exists

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.

_cast(self)

Attempt to cast the data if we're supposed to

_strip(self)

Strips value, if we're supposed to as defined by the strip karg.

_validate_dependencies(self, values)

Validates dependencies, if any. Loop over the items in the dependents list, if the regex matches attempt validation of the form, bubble up the results. Otherwise, ignore the form.

_validate_function(self)

Call the function callback with self.value if it is defined. Function may also be a list of functions. The function should return True on success, False on failure. Function should not raise any exceptions that it does not catch itself.

_validate_regex(self)

Attempt to match against the regex object, if it exists


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