Quixote Widget Classes ====================== [This is reference documentation. If you haven't yet read "Lesson 5: widgets" of demo.txt, you should go and do so now. This document also assumes you have a good understanding of HTML forms and form elements. If not, you could do worse than pick up a copy of *HTML: The Definitive Guide* by Chuck Musciano & Bill Kennedy (O'Reilly). I usually keep it within arm's reach.] Web forms are built out of form elements: string input, select lists, checkboxes, submit buttons, and so forth. Quixote provides a family of classes for handling these form elements, or widgets, in the quixote.form.widget module. The class hierarchy is:: Widget [A] | +--StringWidget | | | +--PasswordWidget | | | +--NumberWidget [*] [A] | | | +-FloatWidget [*] | +-IntWidget [*] | +--TextWidget | +--CheckboxWidget | +--SelectWidget [A] | | | +--SingleSelectWidget | | | | | +-RadiobuttonsWidget | | | | | +-OptionSelectWidget [*] | | | +--MultipleSelectWidget | +--ButtonWidget | | | +-SubmitWidget | | | +-ResetWidget | +--HiddenWidget | +--CompositeWidget [A] | +-WidgetList [*] | +-WidgetDict [*] [*] Widget classes that do not correspond exactly with a particular HTML form element [A] Abstract classes Widget: the base class ---------------------- Widget is the abstract base class for the widget hierarchy. It provides the following facilities: * widget name (``name`` attribute, ``set_name()`` method) * widget value (``value`` attribute, ``set_value()`` and ``clear()`` methods) * ``__str__()`` and ``__repr__()`` methods * some facilities for writing composite widget classes The Widget constructor signature is:: Widget(name : string, value : any = None) ``name`` the name of the widget. For non-compound widgets (ie. everything in the above class hierarchy), this will be used as the "name" attribute for the main HTML tag that defines the form element. ``value`` the current value of the form element. The type of 'value' depends on the widget class. Most widget classes support only a single type, eg. StringWidget always deals with strings and IntWidget with integers. The SelectWidget classes are different; see the descriptions below for details. Common widget methods --------------------- The Widget base class also provides a couple of useful methods: ``set_value(value:any)`` use this to set the widget value; this is the same as supplying a value to the constructor (and the same type rules apply, ie. the type of 'value' depends on the widget class). The following two methods will be used on every widget object you create; if you write your own widget classes, you will almost certainly have to define both of these: ``render()`` : ``string`` return a chunk of HTML that implements the form element corresponding to this widget. ``parse()`` : ``any`` extract the form value for this widget from ``request.form``, parse it according to the rules for this widget class, and return the resulting value. The return value depends on the widget class, and will be of the same type as the value passed to the constructor and/or ``set_value()``. StringWidget ------------ Used for short, single-line string input with no validation (ie. any string will be accepted.) Generates an ```` form element. Constructor ~~~~~~~~~~~ :: StringWidget(name : string, value : string = None, size : int = None, maxlength : int = None) ``size`` used as the ``size`` attribute of the generated ```` tag; controls the physical size of the input field. ``maxlength`` used as the ``maxlength`` attribute; controls the maximum amount of input. Examples ~~~~~~~~ :: >>> StringWidget("foo", value="hello").render() '' >>> StringWidget("foo", size=10, maxlength=20).render() '' PasswordWidget -------------- PasswordWidget is identical to StringWidget except for the type of the HTML form element: ``password`` instead of ``text``. TextWidget ---------- Used for multi-line text input. The value is a single string with newlines right where the browser supplied them. (``\r\n``, if present, is converted to ``\n``.) Generates a ``