form.validators
Attributes
Classes
Wraps a single validator or a list of validators, which will |
|
Validates a string using any python-stdnum format. |
|
Makes sure an uploaded file is not bigger than the given number of |
|
Makes sure an uploaded file is in a whitelist of allowed mimetypes. |
|
Makes sure an uploaded file has one of the expected extensions. Since |
|
Makes sure the given text is a valid onegov.form definition. |
|
Makes sure the given text is a valid onegov.form definition. |
|
Makes sure the given text is a valid onegov.form definition for |
|
A copy of wtform's DataRequired validator, but with a more lax approach |
|
A copy of wtform's Optional validator, but with a more strict approach |
|
Makes sure the given input is valid phone number. |
|
Makes sure the given input is a valid swiss social security number. |
|
Test if the given table does not already have a value in the column |
|
Validator which makes a field required if another field is set and has |
|
Makes sure the selected date is in a valid range. |
Module Contents
- class form.validators.If(condition: FieldCondition[BaseFormT, FieldT], *validators: BaseValidator[BaseFormT, FieldT])[source]
Bases:
Generic
[onegov.form.types.BaseFormT
,onegov.form.types.FieldT
]Wraps a single validator or a list of validators, which will only be executed if the supplied condition callback returns True.
- class form.validators.Stdnum(format: str)[source]
Validates a string using any python-stdnum format.
- class form.validators.FileSizeLimit(max_bytes: int)[source]
Makes sure an uploaded file is not bigger than the given number of bytes.
Expects an
onegov.form.fields.UploadField
oronegov.form.fields.UploadMultipleField
instance.
- class form.validators.WhitelistedMimeType(whitelist: Collection[str] | None = None)[source]
Makes sure an uploaded file is in a whitelist of allowed mimetypes.
Expects an
onegov.form.fields.UploadField
oronegov.form.fields.UploadMultipleField
instance.
- class form.validators.ExpectedExtensions(extensions: Sequence[str])[source]
Bases:
WhitelistedMimeType
Makes sure an uploaded file has one of the expected extensions. Since extensions are not something we can count on we look up the mimetype of the extension and use that to check.
Expects an
onegov.form.fields.UploadField
instance.Usage:
ExpectedExtensions(['*']) # default whitelist ExpectedExtensions(['pdf']) # makes sure the given file is a pdf
- class form.validators.ValidFormDefinition(require_email_field: bool = True, reserved_fields: Collection[str] | None = None, require_title_fields: bool = False, validate_prices: bool = True)[source]
Makes sure the given text is a valid onegov.form definition.
- class form.validators.ValidFilterFormDefinition(require_email_field: bool = True, reserved_fields: Collection[str] | None = None, require_title_fields: bool = False, validate_prices: bool = True)[source]
Bases:
ValidFormDefinition
Makes sure the given text is a valid onegov.form definition.
- class form.validators.ValidSurveyDefinition(require_email_field: bool = False)[source]
Bases:
ValidFormDefinition
Makes sure the given text is a valid onegov.form definition for surveys.
- class form.validators.LaxDataRequired(message=None)[source]
Bases:
wtforms.validators.DataRequired
A copy of wtform’s DataRequired validator, but with a more lax approach to required validation checking. It accepts some specific falsy values, such as numeric falsy values, that would otherwise fail DataRequired.
This is necessary in order for us to validate stored submissions, which get validated after the initial submission in order to avoid losing file uploads.
- class form.validators.StrictOptional(strip_whitespace=True)[source]
Bases:
wtforms.validators.Optional
A copy of wtform’s Optional validator, but with a more strict approach to optional validation checking.
- class form.validators.ValidPhoneNumber(country: str = 'CH', country_whitelist: Collection[str] | None = None)[source]
Makes sure the given input is valid phone number.
Expects an
wtforms.StringField
instance.
- class form.validators.ValidSwissSocialSecurityNumber[source]
Makes sure the given input is a valid swiss social security number.
Expects an
wtforms.StringField
instance.
- class form.validators.UniqueColumnValue(table: type[onegov.core.orm.Base])[source]
Test if the given table does not already have a value in the column (identified by the field name).
If the form provides a model with such an attribute, we allow this value, too.
Usage:
username = StringField(validators=[UniqueColumnValue(User)])
- class form.validators.InputRequiredIf(field_name: str, field_data: object, message: str | None = None)[source]
Bases:
wtforms.validators.InputRequired
Validator which makes a field required if another field is set and has the given value.
- class form.validators.ValidDateRange(min: datetime.date | dateutil.relativedelta.relativedelta | None = None, max: datetime.date | dateutil.relativedelta.relativedelta | None = None, message: str | None = None)[source]
Makes sure the selected date is in a valid range.
The default error message can be overriden and be parametrized with
min_date
andmax_date
if both are supplied or just withdate
if only one of them is specified.