formbase Module
Classes to create form widgets.
Copyright (c) 2008 Christopher Perkins Original Version by Christopher Perkins 2008 Released under MIT license.
Bases: sprox.viewbase.ViewBase
Modifiers : |
---|
Modifiers defined in this class
Name | Description | Default |
---|---|---|
__base_widget_type__ | What widget to use for the form. | TableForm |
__widget_selector_type__ | What class to use for widget selection. | SAWidgetSelector |
__validator_selector_type__ | What class to use for validator selection. | SAValidatorSelector |
__require_fields__ | Specifies which fields are required. | [] |
__check_if_unique__ | Set this to True for “new” forms. This causes Sprox to check if there is an existing record in the database which matches the field data. | False |
__field_validators__ | A dictionary of validators indexed by fieldname. | {} |
__field_validator_types__ | Types of validators to use for each field (allow sprox to set the attribute of the validators). | {} |
__base_validator__ | A validator to attch to the form. | None |
__validator_selector__ | What object to use to select field validators. | None |
__metadata_type__ | What metadata type to use to get schema info on this object | FieldsMetadata |
__dropdown_field_names__ | list or dict of names to use for discovery of field names for dropdowns (None uses sprox default names.) a dict provides field-level granularity | None |
Modifiers inherited from sprox.viewbase.ViewBase
Name | Description | Default |
---|---|---|
__field_widgets__ | A dictionary of widgets to replace the ones that would be chosen by the selector | {} |
__field_widget_types__ | A dictionary of types of widgets, allowing sprox to determine the widget args | {} |
__widget_selector__ | an instantiated object to use for widget selection. | None |
Modifiers inherited from sprox.configbase.ConfigBase
Example Usage: |
---|
One of the more useful things sprox does for you is to fill in the arguments to a drop down automatically. Here is the userform, limited to just the town field, which gets populated with the towns.
>>> from sprox.formbase import FormBase
>>> class UserOnlyTownForm(FormBase):
... __model__ = User
... __limit_fields__ = ['town']
>>>
>>> town_form = UserOnlyTownForm(session)
>>>
>>> print town_form()
<form action="" method="post" class="required tableform">
<div>
<input type="hidden" id="sprox_id" class="hiddenfield" name="sprox_id" value="" />
</div>
<table border="0" cellspacing="0" cellpadding="2" >
<tr class="even" id="town.container" title="" >
<td class="labelcol">
<label id="town.label" for="town" class="fieldlabel">Town</label>
</td>
<td class="fieldcol" >
<select name="town" class="propertysingleselectfield" id="town">
<option value="1">Arvada</option>
<option value="2">Denver</option>
<option value="3">Golden</option>
<option value="4">Boulder</option>
<option value="" selected="selected">-----------</option>
</select>
</td>
</tr>
<tr class="odd" id="submit.container" title="" >
<td class="labelcol">
<label id="submit.label" for="submit" class="fieldlabel"></label>
</td>
<td class="fieldcol" >
<input type="submit" class="submitbutton" value="Submit" />
</td>
</tr>
</table>
</form>
Forms created with sprox can be validated as you would any other widget. >>> class UserOnlyTownForm(FormBase): ... __model__ = User ... __limit_fields__ = [‘town’] ... __required_fields__ = [‘town’] >>> town_form = UserOnlyTownForm(session) >>> town_form.validate(params={‘sprox_id’:1}) Traceback (most recent call last): ... Invalid: town: Missing value
A pass-thru to the widget’s validate function.
Bases: sprox.formbase.FormBase
An editable form who’s purpose is record addition.
Modifiers : |
---|
Name | Description | Default |
---|---|---|
__check_if_unique__ | Set this to True for “new” forms. This causes Sprox to check if there is an existing record in the database which matches the field data. | True |
Here is an example registration form, as generated from the vase User model.
>>> from sprox.formbase import AddRecordForm
>>> from formencode import Schema
>>> from formencode.validators import FieldsMatch
>>> from tw.forms import PasswordField, TextField
>>> form_validator = Schema(chained_validators=(FieldsMatch('password',
... 'verify_password',
... messages={'invalidNoMatch':
... 'Passwords do not match'}),))
>>> class RegistrationForm(AddRecordForm):
... __model__ = User
... __require_fields__ = ['password', 'user_name', 'email_address']
... __omit_fields__ = ['_password', 'groups', 'created', 'user_id', 'town']
... __field_order__ = ['user_name', 'email_address', 'display_name', 'password', 'verify_password']
... __base_validator__ = form_validator
... email_address = TextField
... display_name = TextField
... verify_password = PasswordField('verify_password')
>>> registration_form = RegistrationForm()
>>> print registration_form()
<form action="" method="post" class="required tableform">
<div>
<input type="hidden" id="sprox_id" class="hiddenfield" name="sprox_id" value="" />
</div>
<table border="0" cellspacing="0" cellpadding="2" >
<tr class="even" id="user_name.container" title="" >
<td class="labelcol">
<label id="user_name.label" for="user_name" class="fieldlabel required">User Name</label>
</td>
<td class="fieldcol" >
<input type="text" id="user_name" class="textfield required" name="user_name" value="" />
</td>
</tr>
<tr class="odd" id="email_address.container" title="" >
<td class="labelcol">
<label id="email_address.label" for="email_address" class="fieldlabel required">Email Address</label>
</td>
<td class="fieldcol" >
<input type="text" id="email_address" class="textfield required" name="email_address" value="" />
</td>
</tr>
<tr class="even" id="display_name.container" title="" >
<td class="labelcol">
<label id="display_name.label" for="display_name" class="fieldlabel">Display Name</label>
</td>
<td class="fieldcol" >
<input type="text" id="display_name" class="textfield" name="display_name" value="" />
</td>
</tr>
<tr class="odd" id="password.container" title="" >
<td class="labelcol">
<label id="password.label" for="password" class="fieldlabel required">Password</label>
</td>
<td class="fieldcol" >
<input type="password" id="password" class="required passwordfield" name="password" value="" />
</td>
</tr>
<tr class="even" id="verify_password.container" title="" >
<td class="labelcol">
<label id="verify_password.label" for="verify_password" class="fieldlabel">Verify Password</label>
</td>
<td class="fieldcol" >
<input type="password" id="verify_password" class="passwordfield" name="verify_password" value="" />
</td>
</tr>
<tr class="odd" id="submit.container" title="" >
<td class="labelcol">
<label id="submit.label" for="submit" class="fieldlabel"></label>
</td>
<td class="fieldcol" >
<input type="submit" class="submitbutton" value="Submit" />
</td>
</tr>
</table>
</form>
What is unique about the AddRecord form, is that if the fields in the database are labeled unique, it will automatically vaidate against uniqueness for that field. Here is a simple user form definition, where the user_name in the model is unique:
>>> class AddUserForm(AddRecordForm):
... __entity__ = User
... __limit_fields__ = ['user_name']
>>> user_form = AddUserForm(session)
>>> user_form.validate(params={'sprox_id':'asdf', 'user_name':u'asdf'})
Traceback (most recent call last):
...
Invalid: user_name: That value already exists
The validation fails because there is already a user with the user_name ‘asdf’ in the database
A pass-thru to the widget’s validate function.
Bases: sprox.formbase.FormBase
A form who’s set of fields is disabled.
Modifiers : |
---|
Here is an example disabled form with only the user_name and email fields.
>>> from sprox.test.model import User
>>> from sprox.formbase import DisabledForm
>>> class DisabledUserForm(DisabledForm):
... __model__ = User
... __limit_fields__ = ['user_name', 'email_address']
>>> disabled_user_form = DisabledUserForm()
>>> print disabled_user_form(values=dict(user_name='percious', email='chris@percious.com'))
<form action="" method="post" class="required tableform">
<div>
<input type="hidden" id="user_name" class="hiddenfield" name="user_name" value="" />
<input type="hidden" id="email_address" class="hiddenfield" name="email_address" value="" />
<input type="hidden" id="sprox_id" class="hiddenfield" name="sprox_id" value="" />
</div>
<table border="0" cellspacing="0" cellpadding="2" >
<tr class="even" id="user_name.container" title="" >
<td class="labelcol">
<label id="user_name.label" for="user_name" class="fieldlabel">User Name</label>
</td>
<td class="fieldcol" >
<input type="text" id="user_name" class="textfield" name="user_name" value="" disabled="disabled" />
</td>
</tr>
<tr class="odd" id="email_address.container" title="" >
<td class="labelcol">
<label id="email_address.label" for="email_address" class="fieldlabel">Email Address</label>
</td>
<td class="fieldcol" >
<textarea id="email_address" name="email_address" class="textarea" disabled="disabled" rows="7" cols="50"></textarea>
</td>
</tr>
<tr class="even" id="submit.container" title="" >
<td class="labelcol">
<label id="submit.label" for="submit" class="fieldlabel"></label>
</td>
<td class="fieldcol" >
<input type="submit" class="submitbutton" value="Submit" />
</td>
</tr>
</table>
</form>
You may notice in the above example that disabled fields pass in a hidden value for each disabled field.
A pass-thru to the widget’s validate function.
Bases: sprox.formbase.FormBase
A form for editing a record. :Modifiers:
A pass-thru to the widget’s validate function.