#34385: BaseTemporalField child fields causing AttributeError to be raised by
calling form.is_valid().
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  FabioDavidF                        |
                   Type:  Bug        |         Status:  new
              Component:  Forms      |        Version:  4.1
               Severity:  Normal     |       Keywords:  DateField
                                     |  DateTimeField validation
           Triage Stage:             |  AttributeError
  Unreviewed                         |      Has patch:  0
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 When validating a form with a temporal field that has been passed a non
 `str` native value, `AttributeError` is raised.

 The problem happens in `DateField`, `DateTimeField`, and `TimeField`
 fields. The respective temporal field's `to_python` method treats the
 input assuming it is of type `str`, `datetime.datetime`, `datetime.date`,
 or `datetime.timedelta`. So when something like an integer is fed to a
 field and `clean` is called in the validation process, `AttributeError` is
 raised instead of a `ValidationError`, causing the form not to catch the
 error since it only catches `ValidationError` instances, and, therefore,
 raising an unexpected exception from a, in my opinion, validation error.

 === Reproducing the problem ===
 You can test this through the form validation:
 {{{
 from django import forms

 class MyForm(forms.Form):
     datefield = forms.DateField()

 form = MyForm({'datefield': 1})
 form.is_valid()
 
------------------------------------------------------------------------------------
 >    def to_python(self, value):
 >       value = value.strip()
 E       AttributeError: 'int' object has no attribute 'strip'
 }}}

 Or directly with a field:
 {{{
 from django.forms import DateField

 field = DateField()
 field.to_python(1)
 
------------------------------------------------------------------------------------
 >    def to_python(self, value):
 >       value = value.strip()
 E       AttributeError: 'int' object has no attribute 'strip'
 }}}

 Note that through the form validation process the problem is more
 apparent, since when calling `is_valid`, we do not expect an exception
 raised from said validation, we expect the form to store its validation
 errors so we can handle them directly. Passing an integer to a
 `DateField`, however, causes an exception to be raised when validating a
 form.

 === Test case ===
 {{{
 from django import forms
 from unittest import TestCase


 class TemporalFieldValidationTests(TestCase):
     def
 
test_date_field_raises_validationerror_when_value_is_not_str_or_temporal_object(self):
         date_field = forms.DateField()

         with self.assertRaises(forms.ValidationError):
             date_field.clean(1)

     def
 
test_time_field_raises_validationerror_when_value_is_not_str_or_temporal_object(self):
         time_field = forms.TimeField

         with self.assertRaises(forms.ValidationError):
             time_field.clean(1)

     def
 
test_datetime_field_raises_validationerror_when_value_is_not_str_or_temporal_object(self):
         datetime_field = forms.DateTimeField()

         with self.assertRaises(forms.ValidationError):
             datetime_field.clean(1)
 }}}

 === Considerations ===
 - Tested on versions `3.2.16`, and `4.1.7`
 - I will open a PR to solve this

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34385>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070186a82a44cb-a9129c21-84df-46c0-aff9-ba894ee2b9ac-000000%40eu-central-1.amazonses.com.

Reply via email to