Commits

Mark Lavin committed dcfd9dc

Fix bug with require=True not being respected with allow_new=True. Fixes #91.

Comments (0)

Files changed (3)

docs/releases.rst

 
 - Fixed bug with matching hidden input when the name contains '_1'. Thanks to Augusto Men for the report and fix.
 - Fixed bug where the enter button would open the combobox options rather than submit the form. Thanks to Felipe Prenholato for the report.
+- Fixed bug with using ``allow_new=True`` creating items when no data was submitted. See #91
 
 
 Backwards Incompatible Changes

selectable/forms/fields.py

     def to_python(self, value):
         if value in EMPTY_VALUES:
             return None
-        lookup =self.lookup_class()
+        lookup = self.lookup_class()
         if isinstance(value, list):
             # Input comes from an AutoComplete widget. It's two
             # components: text and id
             if len(value) != 2:
                 raise ValidationError(self.error_messages['invalid_choice'])
-            if value[1] in EMPTY_VALUES:
+            label, pk = value
+            if pk in EMPTY_VALUES:
                 if not self.allow_new:
-                    if value[0]:
+                    if label:
                         raise ValidationError(self.error_messages['invalid_choice'])
                     else:
                         return None
-                value = lookup.create_item(value[0])
+                if label in EMPTY_VALUES:
+                    return None
+                value = lookup.create_item(label)
             else:
-                value = lookup.get_item(value[1])
+                value = lookup.get_item(pk)
                 if value is None:
                     raise ValidationError(self.error_messages['invalid_choice'])
         else:

selectable/tests/functests.py

     'FuncSelectModelChoiceTestCase',
     'FuncComboboxModelChoiceTestCase',
     'FuncManytoManyMultipleSelectTestCase',
+    'FuncFormTestCase',
 )
 
 
         self.assertEqual(manything.name, data['name'])
         things = manything.things.all()
         self.assertEqual(things.count(), 0)
+
+
+class SimpleForm(forms.Form):
+    "Non-model form usage."
+    thing = AutoCompleteSelectField(lookup_class=ThingLookup)
+    new_thing = AutoCompleteSelectField(lookup_class=ThingLookup, allow_new=True)
+    things = AutoCompleteSelectMultipleField(lookup_class=ThingLookup)
+
+
+class FuncFormTestCase(BaseSelectableTestCase):
+    """
+    Functional tests for using AutoCompleteSelectField
+    and AutoCompleteSelectMultipleField outside the context
+    of a ModelForm.
+    """
+
+    def setUp(self):
+        self.test_thing = self.create_thing()
+
+    def test_blank_new_item(self):
+        "Regression test for #91. new_thing is required but both are blank."
+        data = {
+            'thing_0': self.test_thing.name,
+            'thing_1': self.test_thing.pk,
+            'new_thing_0': '',
+            'new_thing_1': '',
+            'things_0': '',
+            'things_1': [self.test_thing.pk, ]
+        }
+        form = SimpleForm(data=data)
+        self.assertFalse(form.is_valid())
+        self.assertTrue('new_thing' in form.errors)