formencode.foreach – Validate items in a list

Validator for repeating items.

Module Contents

class formencode.foreach.ForEach(*args, **kw)

Use this to apply a validator/converter to each item in a list.

For instance:

ForEach(Int(), OneOf([1, 2, 3]))

Will take a list of values and try to convert each of them to an integer, and then check if each integer is 1, 2, or 3. Using multiple arguments is equivalent to:

ForEach(All(Int(), OneOf([1, 2, 3])))

Use convert_to_list=True if you want to force the input to be a list. This will turn non-lists into one-element lists, and None into the empty list. This tries to detect sequences by iterating over them (except strings, which aren’t considered sequences).

ForEach will try to convert the entire list, even if errors are encountered. If errors are encountered, they will be collected and a single Invalid exception will be raised at the end (with error_list set).

If the incoming value is a set, then we return a set.

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)