forked from collective/collective.volto.formsupport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3251e4f
commit 7df5dda
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/collective/volto/formsupport/validation/custom_validators/WordsValidator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import re | ||
|
||
from Products.validation.interfaces.IValidator import IValidator | ||
from zope.interface import implementer | ||
|
||
|
||
@implementer(IValidator) | ||
class WordsValidator: | ||
def __init__( | ||
self, | ||
name, | ||
title="", | ||
description="", | ||
words=0, | ||
_internal_type="", | ||
): | ||
""" "Unused properties are for default values and type information""" | ||
self.name = name | ||
self.title = title or name | ||
self.description = description | ||
self._internal_type = _internal_type | ||
# Default values | ||
self.words = words | ||
|
||
def __call__(self, value="", *args, **kwargs): | ||
words = kwargs.get("words", self.words) | ||
words = int(words) | ||
count = len(re.findall(r"\w+", value)) | ||
|
||
if self._internal_type == "max": | ||
if not value: | ||
return | ||
if count > words: | ||
# TODO: i18n | ||
msg = f"Validation failed({self.name}): is more than {words} words long" | ||
return msg | ||
elif self._internal_type == "min": | ||
if not value or count < words: | ||
# TODO: i18n | ||
msg = f"Validation failed({self.name}): is less than {words} words long" | ||
return msg | ||
elif self._internal_type == "test": | ||
pass | ||
else: | ||
# TODO: i18n | ||
msg = f"Validation failed({self.name}): Unknown words validator type" | ||
return msg |
7 changes: 6 additions & 1 deletion
7
src/collective/volto/formsupport/validation/custom_validators/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
from collective.volto.formsupport.validation.custom_validators.CharactersValidator import ( | ||
CharactersValidator, | ||
) | ||
from collective.volto.formsupport.validation.custom_validators.WordsValidator import ( | ||
WordsValidator, | ||
) | ||
|
||
maxCharacters = CharactersValidator("maxCharacters", _internal_type="max") | ||
minCharacters = CharactersValidator("minCharacters", _internal_type="min") | ||
custom_validators = [maxCharacters, minCharacters] | ||
maxWords = WordsValidator("maxWords", _internal_type="max") | ||
minWords = WordsValidator("minWords", _internal_type="min") | ||
custom_validators = [maxCharacters, minCharacters, maxWords, minWords] |