Validators¶
A mechanism for validating that input meets a given set of criteria.
Usage¶
A validator is a callable that accepts a string as input, and returns None on success, or a string on failure. If a string is returned, that string will be used as an error message. For example, the following example will validate that the user's input starts with the text "Hello":
def must_say_hello(value):
if value.lower().startswith("hello"):
return None
return "Why didn't you say hello?"
Toga provides built-in validators for a range of common validation types, as well as some base classes that can be used as a starting point for custom validators.
A list of validators can then be provided to any widget that performs validation, such as the TextInput widget. In the following example, a TextInput will validate that the user has entered text that starts with "hello", and has provided at least 10 characters of input:
import toga
from toga.validators import MinLength
widget = toga.TextInput(validators=[
must_say_hello,
MinLength(10)
])
Whenever the input changes, all validators will be evaluated in the order they have been specified. The first validator to fail will put the widget into an "error" state, and the error message returned by that validator will be displayed to the user.
Reference¶
toga.validators.BooleanValidator ¶
BooleanValidator(error_message, allow_empty=True)
An abstract base class for defining a simple validator.
Subclasses should implement the is_valid() method
| PARAMETER | DESCRIPTION |
|---|---|
error_message
|
The error to display to the user when the input isn't valid.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.CountValidator ¶
CountValidator(
count,
expected_existence_message,
expected_non_existence_message,
expected_count_message,
allow_empty=True,
)
An abstract base class for validators that are based on counting instances of some content in the overall content.
Subclasses should implement the count() method to identify the content of
interest.
| PARAMETER | DESCRIPTION |
|---|---|
count
|
Optional; The expected count.
TYPE:
|
expected_existence_message
|
The error message to show if matches are expected, but were not found.
TYPE:
|
expected_non_existence_message
|
The error message to show if matches were not expected, but were found.
TYPE:
|
expected_count_message
|
The error message to show if matches were expected, but a different number were found.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.Contains ¶
Contains(substring, count=None, error_message=None, allow_empty=True)
Bases: CountValidator
A validator confirming that the string contains one or more copies of a substring.
| PARAMETER | DESCRIPTION |
|---|---|
substring
|
The substring that must exist in the input.
TYPE:
|
count
|
Optional; The exact number of matches that are expected.
TYPE:
|
error_message
|
Optional; the error message to display when the input doesn't contain the substring (or the requested count of substrings).
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.ContainsDigit ¶
ContainsDigit(count=None, error_message=None, allow_empty=True)
Bases: CountValidator
A validator confirming that the string contains digits.
| PARAMETER | DESCRIPTION |
|---|---|
count
|
Optional; if provided, the exact count of digits that must exist. If not provided, the existence of any digit will make the string valid.
TYPE:
|
error_message
|
Optional; the error message to display when the input doesn't contain digits (or the requested count of digits).
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.ContainsLowercase ¶
ContainsLowercase(count=None, error_message=None, allow_empty=True)
Bases: CountValidator
A validator confirming that the string contains lower case letters.
| PARAMETER | DESCRIPTION |
|---|---|
count
|
Optional; if provided, the exact count of lower case letters that must exist. If not provided, the existence of any lower case letter will make the string valid.
TYPE:
|
error_message
|
Optional; the error message to display when the input doesn't contain lower case letters (or the requested count of lower case letters).
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.ContainsSpecial ¶
ContainsSpecial(count=None, error_message=None, allow_empty=True)
Bases: CountValidator
A validator confirming that the string contains "special" (i.e., non-alphanumeric) characters.
| PARAMETER | DESCRIPTION |
|---|---|
count
|
Optional; if provided, the exact count of special characters that must exist. If not provided, the existence of any special character will make the string valid.
TYPE:
|
error_message
|
Optional; the error message to display when the input doesn't contain special characters (or the requested count of special characters).
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.ContainsUppercase ¶
ContainsUppercase(count=None, error_message=None, allow_empty=True)
Bases: CountValidator
A validator confirming that the string contains upper case letters.
| PARAMETER | DESCRIPTION |
|---|---|
count
|
Optional; if provided, the exact count of upper case letters that must exist. If not provided, the existence of any upper case letter will make the string valid.
TYPE:
|
error_message
|
Optional; the error message to display when the input doesn't contain upper case letters (or the requested count of upper case letters).
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.Email ¶
Email(error_message=None, allow_empty=True)
Bases: MatchRegex
A validator confirming that the string is an email address.
Note
It's impossible to do true RFC-compliant email validation with a regex. This validator does a "best effort" validation. It will inevitably allow some email addresses that aren't technically valid. However, it shouldn't exclude any valid email addresses.
| PARAMETER | DESCRIPTION |
|---|---|
error_message
|
Optional; the error message to display when the input isn't a number.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.EndsWith ¶
EndsWith(substring, error_message=None, allow_empty=True)
Bases: BooleanValidator
A validator confirming that the string ends with a given substring.
| PARAMETER | DESCRIPTION |
|---|---|
substring
|
The substring that the input must end with.
TYPE:
|
error_message
|
Optional; the error message to display when the string doesn't end with the given substring.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.Integer ¶
Integer(error_message=None, allow_empty=True)
Bases: BooleanValidator
A validator confirming that the string is an integer.
| PARAMETER | DESCRIPTION |
|---|---|
error_message
|
Optional; the error message to display when the input isn't an integer.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.LengthBetween ¶
LengthBetween(min_length, max_length, error_message=None, allow_empty=True)
Bases: BooleanValidator
A validator confirming that the length of input falls in a given range.
| PARAMETER | DESCRIPTION |
|---|---|
min_length
|
The minimum length of the string (inclusive).
TYPE:
|
max_length
|
The maximum length of the string (inclusive).
TYPE:
|
error_message
|
Optional; the error message to display when the length isn't in the given range.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.MatchRegex ¶
MatchRegex(regex_string, error_message=None, allow_empty=True)
Bases: BooleanValidator
A validator confirming that the string matches a given regular expression.
| PARAMETER | DESCRIPTION |
|---|---|
regex_string
|
A regular expression that the input must match.
TYPE:
|
error_message
|
Optional; the error message to display when the input doesn't match the provided regular expression.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.MaxLength ¶
MaxLength(length, error_message=None)
Bases: LengthBetween
A validator confirming that the length of input does not exceed a maximum size.
| PARAMETER | DESCRIPTION |
|---|---|
length
|
The maximum length of the string (inclusive).
TYPE:
|
error_message
|
Optional; the error message to display when the string is too long.
TYPE:
|
toga.validators.MinLength ¶
MinLength(length, error_message=None, allow_empty=True)
Bases: LengthBetween
A validator confirming that the length of input exceeds a minimum size.
| PARAMETER | DESCRIPTION |
|---|---|
length
|
The minimum length of the string (inclusive).
TYPE:
|
error_message
|
Optional; the error message to display when the string isn't long enough.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.NotContains ¶
NotContains(substring, error_message=None)
Bases: Contains
A validator confirming that the string does not contain a substring.
| PARAMETER | DESCRIPTION |
|---|---|
substring
|
A substring that must not exist in the input.
TYPE:
|
error_message
|
Optional; the error message to display when the input contains the provided substring.
TYPE:
|
toga.validators.Number ¶
Number(error_message=None, allow_empty=True)
Bases: BooleanValidator
A validator confirming that the string is a number.
| PARAMETER | DESCRIPTION |
|---|---|
error_message
|
Optional; the error message to display when the input isn't a number.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|
toga.validators.StartsWith ¶
StartsWith(substring, error_message=None, allow_empty=True)
Bases: BooleanValidator
A validator confirming that the input starts with a given substring.
| PARAMETER | DESCRIPTION |
|---|---|
substring
|
The substring that the input must start with.
TYPE:
|
error_message
|
Optional; the error message to display when the string doesn't start with the given substring.
TYPE:
|
allow_empty
|
Optional; Is no input considered valid? Defaults to
TYPE:
|