mirror of
https://github.com/django/django.git
synced 2025-11-28 14:37:59 +00:00
Fixed #28201 -- Added ProhibitNullCharactersValidator and used it on CharField form field.
This commit is contained in:
parent
b78d100fa6
commit
90d7b912b9
6 changed files with 88 additions and 6 deletions
|
|
@ -503,3 +503,27 @@ def get_available_image_extensions():
|
|||
validate_image_file_extension = FileExtensionValidator(
|
||||
allowed_extensions=get_available_image_extensions(),
|
||||
)
|
||||
|
||||
|
||||
@deconstructible
|
||||
class ProhibitNullCharactersValidator:
|
||||
"""Validate that the string doesn't contain the null character."""
|
||||
message = _('Null characters are not allowed.')
|
||||
code = 'null_characters_not_allowed'
|
||||
|
||||
def __init__(self, message=None, code=None):
|
||||
if message is not None:
|
||||
self.message = message
|
||||
if code is not None:
|
||||
self.code = code
|
||||
|
||||
def __call__(self, value):
|
||||
if '\x00' in str(value):
|
||||
raise ValidationError(self.message, code=self.code)
|
||||
|
||||
def __eq__(self, other):
|
||||
return (
|
||||
isinstance(other, self.__class__) and
|
||||
self.message == other.message and
|
||||
self.code == other.code
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue