Ticket #4386: newforms-cleanmethod.diff

File newforms-cleanmethod.diff, 1009 bytes (added by Thomas Güttler <hv@…>, 17 years ago)
  • docs/newforms.txt

     
    301301empty values as an empty string. Each field type knows what its "blank" value
    302302is -- e.g., for ``DateField``, it's ``None`` instead of the empty string.
    303303
     304Custom Validation and Cleaning:
     305-------------------------------
     306
     307You can write custom validation methods. The method must be called
     308``clean_FIELDNAME``. It is possible to alter the value, too. Example:
     309
     310    >>> class OptionalPersonForm(Form):
     311    ...     first_name = CharField()
     312    ...     def clean_first_name(self):
     313    ...         value = self.fields["first_name"].widget.value_from_datadict(self.data, self.add_prefix("first_name"))
     314    ...         if not re.match(r'^[a-zA-Z]+$', value):
     315    ...             raise ValidationError("Only a-z A-Z allowed.")
     316    ...         return value
     317
     318
    304319Behavior of unbound forms
    305320~~~~~~~~~~~~~~~~~~~~~~~~~
    306321
Back to Top