From 6168382f1f2d038963ba4d70c356c375c7647fa5 Mon Sep 17 00:00:00 2001
From: Dmitriy Korneev <lagovas.lagovas@gmail.com>
Date: Wed, 24 Sep 2014 14:09:30 +0300
Subject: [PATCH] fix BooleanField (ticket #23547) in case when field is
required and have valid false value fix tests for BooleanField
---
django/forms/fields.py | 4 +++-
tests/forms_tests/tests/test_fields.py | 3 ---
2 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 966b303..035055d 100644
a
|
b
|
class BooleanField(Field):
|
696 | 696 | # will submit for False. Also check for '0', since this is what |
697 | 697 | # RadioSelect will provide. Because bool("True") == bool('1') == True, |
698 | 698 | # we don't need to handle that explicitly. |
| 699 | self.raw_value = value |
699 | 700 | if isinstance(value, six.string_types) and value.lower() in ('false', '0'): |
700 | 701 | value = False |
701 | 702 | else: |
… |
… |
class BooleanField(Field):
|
703 | 704 | return super(BooleanField, self).to_python(value) |
704 | 705 | |
705 | 706 | def validate(self, value): |
706 | | if not value and self.required: |
| 707 | if self.raw_value in self.empty_values and self.required: |
707 | 708 | raise ValidationError(self.error_messages['required'], code='required') |
| 709 | del self.raw_value |
708 | 710 | |
709 | 711 | def _has_changed(self, initial, data): |
710 | 712 | # Sometimes data or initial could be None or '' which should be the |
diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py
index 262ec5d..491f01e 100644
a
|
b
|
class FieldsTests(SimpleTestCase):
|
850 | 850 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '') |
851 | 851 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) |
852 | 852 | self.assertEqual(True, f.clean(True)) |
853 | | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, False) |
854 | 853 | self.assertEqual(True, f.clean(1)) |
855 | | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, 0) |
856 | 854 | self.assertEqual(True, f.clean('Django rocks')) |
857 | 855 | self.assertEqual(True, f.clean('True')) |
858 | | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, 'False') |
859 | 856 | |
860 | 857 | def test_booleanfield_2(self): |
861 | 858 | f = BooleanField(required=False) |