Forms¶
- class keg_elements.forms.FieldMeta(label_text=(), description=(), label_modifier=(), choices_modifier=(), choices=None, required=(), widget=(), extra_validators=(), coerce=(), default=())¶
Meta information for fields to override model-generated info.
Rather than forcing all-or-nothing acceptance of model-generated meta info from wtforms, FieldMeta may be provided in the FieldsMeta nested class of the form to override specifics.
All modifications are applied to the field instance during the form generation process.
Example:
class PersonForm(ModelForm): class Meta: model = Person class FieldsMeta: name = FieldMeta('Full Name')
- Parameters:
label_text – Force a label value.
description – Force a description value.
label_modifier – Callable to be called with the default label. label_text takes precedence if both are provided. But, this modifier will apply to choices as well if applicable and a choices_modifier is not given.
choices_modifier – Callable to be called with the label value for choices.
required – Force validators to be added/removed for requirement.
widget – Force a specific widget to be used for the field in render.
extra_validators – Add the given validators to those existing on the field.
coerce – Applies a specific coerce for field values. Applicable to select fields only.
default – Forces a default value on the field.
- modify_description(description)¶
for subclasses to easily modify the final label text value
- modify_label(label_text)¶
for subclasses to easily modify the final label text value
- class keg_elements.forms.Form(*args, **kwargs)¶
Base form with a bunch of QoL improvements
- Parameters:
_field_order –
Relying on the default field ordering can lead to unintuitive forms. It is possible to override this by adding the
_field_order
class attribute. Set this class variable to a tuple or list of field names (addressable via Form._fields[‘name_of_field’]) and the form will render in that order. You must include all the fields, except CSRF. Forgetting a field or adding one which doesn’t exist will cause the form to raise aValueError
and the form will not be rendered.- class MyForm(Form):
_field_order = (‘field1’, ‘field2’,)
field1 = String(‘field1_label’) # Note that we don’t use the label in the ordering field2 = String()
- after_init(args, kwargs)¶
Hook for providing customization on the form after fields are initialized.
- property field_errors¶
Field-level validator errors come from WTForms’ errors.
- fields_todict()¶
Turns a form into dicts and lists with both data and errors for each field.
- validate()¶
Applies validators and returns bool.
Methods decorated as form-level validators are run after WTForms generic validation.
- class keg_elements.forms.FormGenerator(form_class)¶
Model form generator that applies field meta info, provides validators, etc.
Meta nested class directives (in addition to wtforms-alchemy): - include_datetimes_with_default - include_required_foreign_keys
Field class overrides: - Use our SelectField instead of the WTForms default - Use our RequiredBoolRadioField for non-nullable boolean fields - Use RelationshipField for foreign key fields
Meta info modifiers: - Use FieldsMeta.<field_name> if provided - Falls back to FieldsMeta.__default__ - If none of the above, uses a blank FieldsMeta object, which will title case the label.
Validators: - Applies range/scale numeric validators when applicable.
- create_field(prop, column)¶
Create form field for given column.
- Parameters:
prop – SQLAlchemy ColumnProperty object.
column – SQLAlchemy Column object.
- create_validators(prop, column)¶
Returns validators for given column
- Parameters:
column – SQLAlchemy Column object
- get_field_class(column)¶
Returns WTForms field class. Class is based on a custom field class attribute or SQLAlchemy column type.
- Parameters:
column – SQLAlchemy Column object
- length_validator(column)¶
Returns length validator for given column
- Parameters:
column – SQLAlchemy Column object
- select_field_kwargs(column)¶
Returns key value args for SelectField based on SQLAlchemy column definitions.
- Parameters:
column – SQLAlchemy Column object
- skip_column(column)¶
Whether or not to skip column in the generation process.
- Parameters:
column_property – SQLAlchemy Column object