summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshesh <aksheshdoshi@gmail.com>2016-02-16 03:40:00 +0530
committerTim Graham <timograham@gmail.com>2016-02-18 18:58:18 -0500
commitd58aaa24e31f10e56a7f05a4451cd06a3cc6e65d (patch)
tree75b2a1d79cd855a4f8fce44212624f466cf9a7b9
parentb954ad0640e1f246f60f31a07a567274c2f20751 (diff)
downloaddjango-d58aaa24e31f10e56a7f05a4451cd06a3cc6e65d.tar.gz
Fixed #26107 -- Added option to int_list_validator() to allow negative integers.
-rw-r--r--django/core/validators.py7
-rw-r--r--docs/ref/validators.txt11
-rw-r--r--docs/releases/1.10.txt4
-rw-r--r--tests/validators/tests.py5
4 files changed, 22 insertions, 5 deletions
diff --git a/django/core/validators.py b/django/core/validators.py
index dd18aee3cb..5e6265e507 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -283,8 +283,11 @@ def ip_address_validators(protocol, unpack_ipv4):
% (protocol, list(ip_address_validator_map)))
-def int_list_validator(sep=',', message=None, code='invalid'):
- regexp = _lazy_re_compile('^\d+(?:%s\d+)*\Z' % re.escape(sep))
+def int_list_validator(sep=',', message=None, code='invalid', allow_negative=False):
+ regexp = _lazy_re_compile('^%(neg)s\d+(?:%(sep)s%(neg)s\d+)*\Z' % {
+ 'neg': '(-)?' if allow_negative else '',
+ 'sep': re.escape(sep),
+ })
return RegexValidator(regexp, message=message, code=code)
diff --git a/docs/ref/validators.txt b/docs/ref/validators.txt
index 13832cfa46..f6feca58c9 100644
--- a/docs/ref/validators.txt
+++ b/docs/ref/validators.txt
@@ -226,12 +226,17 @@ to, or in lieu of custom ``field.clean()`` methods.
``int_list_validator``
----------------------
-.. function:: int_list_validator(sep=',', message=None, code='invalid')
+.. function:: int_list_validator(sep=',', message=None, code='invalid', allow_negative=False)
.. versionadded:: 1.9
- Returns a :class:`RegexValidator` instance that ensures a string
- consists of integers separated by ``sep``.
+ Returns a :class:`RegexValidator` instance that ensures a string consists
+ of integers separated by ``sep``. It allows negative integers when
+ ``allow_negative`` is ``True``.
+
+ .. versionchanged:: 1.10
+
+ The ``allow_negative`` parameter was added.
``MaxValueValidator``
---------------------
diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt
index 1154429f2b..b3f14731d3 100644
--- a/docs/releases/1.10.txt
+++ b/docs/releases/1.10.txt
@@ -355,6 +355,10 @@ Validators
domain name labels to 63 characters and the total length of domain
names to 253 characters per :rfc:`1034`.
+* :func:`~django.core.validators.int_list_validator` now accepts an optional
+ ``allow_negative`` boolean parameter, defaulting to ``False``, to allow
+ negative integers.
+
Backwards incompatible changes in 1.10
======================================
diff --git a/tests/validators/tests.py b/tests/validators/tests.py
index ad82eb6132..3266b5832b 100644
--- a/tests/validators/tests.py
+++ b/tests/validators/tests.py
@@ -172,6 +172,11 @@ TEST_DATA = [
(validate_comma_separated_integer_list, '1,,2', ValidationError),
(int_list_validator(sep='.'), '1.2.3', None),
+ (int_list_validator(sep='.', allow_negative=True), '1.2.3', None),
+ (int_list_validator(allow_negative=True), '-1,-2,3', None),
+ (int_list_validator(allow_negative=True), '1,-2,-12', None),
+
+ (int_list_validator(), '-1,2,3', ValidationError),
(int_list_validator(sep='.'), '1,2,3', ValidationError),
(int_list_validator(sep='.'), '1.2.3\n', ValidationError),