Fixed #35449 -- Fixed validation of array items in SplitArrayField when remove_trailing_nulls=True.

This commit is contained in:
GappleBee 2024-09-14 10:26:12 +01:00 committed by Sarah Boyce
parent 50f89ae850
commit a417c0efb4
2 changed files with 17 additions and 1 deletions

View File

@ -228,7 +228,7 @@ class SplitArrayField(forms.Field):
params={"nth": index + 1},
)
)
cleaned_data.append(None)
cleaned_data.append(item)
else:
errors.append(None)
cleaned_data, null_index = self._remove_trailing_nulls(cleaned_data)

View File

@ -1339,6 +1339,22 @@ class TestSplitFormField(PostgreSQLSimpleTestCase):
],
)
def test_invalid_char_length_with_remove_trailing_nulls(self):
field = SplitArrayField(
forms.CharField(max_length=2, required=False),
size=3,
remove_trailing_nulls=True,
)
with self.assertRaises(exceptions.ValidationError) as cm:
field.clean(["abc", "", ""])
self.assertEqual(
cm.exception.messages,
[
"Item 1 in the array did not validate: Ensure this value has at most 2 "
"characters (it has 3).",
],
)
def test_splitarraywidget_value_omitted_from_data(self):
class Form(forms.ModelForm):
field = SplitArrayField(forms.IntegerField(), required=False, size=2)