Skip to content

Commit

Permalink
Fixed #35449 -- Fixed validation of array items in SplitArrayField wh…
Browse files Browse the repository at this point in the history
…en remove_trailing_nulls=True.
  • Loading branch information
GappleBee authored and sarahboyce committed Oct 7, 2024
1 parent 50f89ae commit a417c0e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/contrib/postgres/forms/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def clean(self, value):
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)
Expand Down
16 changes: 16 additions & 0 deletions tests/postgres_tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,22 @@ def test_invalid_char_length(self):
],
)

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)
Expand Down

0 comments on commit a417c0e

Please sign in to comment.