Django

Code

Show
Ignore:
Timestamp:
11/04/06 14:49:59 (3 years ago)
Author:
adrian
Message:

django.newforms: Implemented hook for validation not tied to a particular field. Renamed to_python() to clean() -- it's just...cleaner. Added Form.as_table(), Form.as_url(), Form.as_table_with_errors() and Form.as_ul_with_errors(). Added ComboField?. Updated all unit tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/fields.py

    r3959 r3978  
    1515    'RegexField', 'EmailField', 'URLField', 'BooleanField', 
    1616    'ChoiceField', 'MultipleChoiceField', 
     17    'ComboField', 
    1718) 
    1819 
     
    3536        self.widget = widget 
    3637 
    37     def to_python(self, value): 
    38         """ 
    39         Validates the given value and returns its "normalized" value as an 
     38    def clean(self, value): 
     39        """ 
     40        Validates the given value and returns its "cleaned" value as an 
    4041        appropriate Python object. 
    4142 
     
    5152        self.max_length, self.min_length = max_length, min_length 
    5253 
    53     def to_python(self, value): 
     54    def clean(self, value): 
    5455        "Validates max_length and min_length. Returns a Unicode object." 
    55         Field.to_python(self, value) 
     56        Field.clean(self, value) 
    5657        if value in EMPTY_VALUES: value = u'' 
    5758        if not isinstance(value, basestring): 
     
    6667 
    6768class IntegerField(Field): 
    68     def to_python(self, value): 
     69    def clean(self, value): 
    6970        """ 
    7071        Validates that int() can be called on the input. Returns the result 
    7172        of int(). 
    7273        """ 
    73         super(IntegerField, self).to_python(value) 
     74        super(IntegerField, self).clean(value) 
    7475        try: 
    7576            return int(value) 
     
    9091        self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS 
    9192 
    92     def to_python(self, value): 
     93    def clean(self, value): 
    9394        """ 
    9495        Validates that the input can be converted to a date. Returns a Python 
    9596        datetime.date object. 
    9697        """ 
    97         Field.to_python(self, value) 
     98        Field.clean(self, value) 
    9899        if value in EMPTY_VALUES: 
    99100            return None 
     
    126127        self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS 
    127128 
    128     def to_python(self, value): 
     129    def clean(self, value): 
    129130        """ 
    130131        Validates that the input can be converted to a datetime. Returns a 
    131132        Python datetime.datetime object. 
    132133        """ 
    133         Field.to_python(self, value) 
     134        Field.clean(self, value) 
    134135        if value in EMPTY_VALUES: 
    135136            return None 
     
    158159        self.error_message = error_message or u'Enter a valid value.' 
    159160 
    160     def to_python(self, value): 
     161    def clean(self, value): 
    161162        """ 
    162163        Validates that the input matches the regular expression. Returns a 
    163164        Unicode object. 
    164165        """ 
    165         Field.to_python(self, value) 
     166        Field.clean(self, value) 
    166167        if value in EMPTY_VALUES: value = u'' 
    167168        if not isinstance(value, basestring): 
     
    193194        self.verify_exists = verify_exists 
    194195 
    195     def to_python(self, value): 
    196         value = RegexField.to_python(self, value) 
     196    def clean(self, value): 
     197        value = RegexField.clean(self, value) 
    197198        if self.verify_exists: 
    198199            import urllib2 
     
    208209    widget = CheckboxInput 
    209210 
    210     def to_python(self, value): 
     211    def clean(self, value): 
    211212        "Returns a Python boolean object." 
    212         Field.to_python(self, value) 
     213        Field.clean(self, value) 
    213214        return bool(value) 
    214215 
     
    220221        self.choices = choices 
    221222 
    222     def to_python(self, value): 
     223    def clean(self, value): 
    223224        """ 
    224225        Validates that the input is in self.choices. 
    225226        """ 
    226         value = Field.to_python(self, value) 
     227        value = Field.clean(self, value) 
    227228        if value in EMPTY_VALUES: value = u'' 
    228229        if not isinstance(value, basestring): 
     
    239240        ChoiceField.__init__(self, choices, required, widget) 
    240241 
    241     def to_python(self, value): 
     242    def clean(self, value): 
    242243        """ 
    243244        Validates that the input is a list or tuple. 
     
    260261                raise ValidationError(u'Select a valid choice. %s is not one of the available choices.' % val) 
    261262        return new_value 
     263 
     264class ComboField(Field): 
     265    def __init__(self, fields=(), required=True, widget=None): 
     266        Field.__init__(self, required, widget) 
     267        self.fields = fields 
     268 
     269    def clean(self, value): 
     270        """ 
     271        Validates the given value against all of self.fields, which is a 
     272        list of Field instances. 
     273        """ 
     274        Field.clean(self, value) 
     275        for field in self.fields: 
     276            value = field.clean(value) 
     277        return value