Commit c490e410 authored by Marc Tamlyn's avatar Marc Tamlyn
Browse files

Fixed #24373 -- Added run_validators to ArrayField.

Thanks to DavidMuller for the report.
parent 32d4db66
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -139,6 +139,18 @@ class ArrayField(Field):
                    code='nested_array_mismatch',
                )

    def run_validators(self, value):
        super(ArrayField, self).run_validators(value)
        for i, part in enumerate(value):
            try:
                self.base_field.run_validators(part)
            except exceptions.ValidationError as e:
                raise exceptions.ValidationError(
                    string_concat(self.error_messages['item_invalid'], ' '.join(e.messages)),
                    code='item_invalid',
                    params={'nth': i},
                )

    def formfield(self, **kwargs):
        defaults = {
            'form_class': SimpleArrayField,
+9 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ import uuid
from django import forms
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.forms import SimpleArrayField, SplitArrayField
from django.core import exceptions, serializers
from django.core import exceptions, serializers, validators
from django.core.management import call_command
from django.db import IntegrityError, connection, models
from django.test import TestCase, TransactionTestCase, override_settings
@@ -330,6 +330,14 @@ class TestValidation(TestCase):
        self.assertEqual(cm.exception.code, 'nested_array_mismatch')
        self.assertEqual(cm.exception.messages[0], 'Nested arrays must have the same length.')

    def test_with_validators(self):
        field = ArrayField(models.IntegerField(validators=[validators.MinValueValidator(1)]))
        field.clean([1, 2], None)
        with self.assertRaises(exceptions.ValidationError) as cm:
            field.clean([0], None)
        self.assertEqual(cm.exception.code, 'item_invalid')
        self.assertEqual(cm.exception.messages[0], 'Item 0 in the array did not validate: Ensure this value is greater than or equal to 1.')


class TestSimpleFormField(TestCase):