From a417c0efb4b37fe9f59d7b982b2ecf276bf2e306 Mon Sep 17 00:00:00 2001 From: GappleBee Date: Sat, 14 Sep 2024 10:26:12 +0100 Subject: [PATCH] Fixed #35449 -- Fixed validation of array items in SplitArrayField when remove_trailing_nulls=True. --- django/contrib/postgres/forms/array.py | 2 +- tests/postgres_tests/test_array.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/django/contrib/postgres/forms/array.py b/django/contrib/postgres/forms/array.py index ddb022afc3..fd5cd219f8 100644 --- a/django/contrib/postgres/forms/array.py +++ b/django/contrib/postgres/forms/array.py @@ -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) diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index ff0c4aabb1..ea7807687e 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -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)