Validating and cleaning data ============================= Processors ---------- Before validation, any processing functions are run. These allow you do simple cleaning operations (eg calling `strip` on string values) before any validation rules are checked. Processors can be configured by setting :attr:`~morf.fields.Field.processors`: .. code:: python class MyForm(Form): email = fields.Str(processors=[lambda s: s.lower().strip()]) Validators ---------- Morf includes various common validator functions that can be configured by setting :attr:`~morf.fields.Field.validators`: .. code:: python from morf import Form, fields, validators class MyForm(Form): email = fields.Str(validators=[validators.is_email()]) message = fields.Str(validators=[validators.minlen(10)]) Refer to :mod:`~morf.validators` for the list of built-in validation functions. Ad-hoc validation functions --------------------------- :meth:`~morf.form.validates` lets you create ad-hoc validation functions: .. code:: python from morf import Form from morf import validates class MyForm(Form): number1 = fields.Int() number2 = fields.Int() @validates(number1) def check_first_number_is_even(self, n1): if n1 % 2 != 0: self.fail("Even numbers only!") @validates(number1, number2) def check_second_number_is_multiple_of_first(self, n1, n2): if n2 % n1 != 0 self.fail("Enter a multiple of the first number") @cleans(word) def clean_word(self, word): return word.lower() :meth:`~morf.form.cleans` works in a similar way for data cleaning functions, which are expected to return the cleaned data: .. code:: python from morf import Form from morf import cleans class MyForm(Form): word = fields.Str() @cleans(word) def clean_word(self, word): return word.lower() A validator or cleaner may also act on the whole form object: .. code:: python @cleans def clean_everything(self): self.data['word'] = self.data['word'].lower() Validators reference --------------------- .. automodule:: morf.validators :members: