Add min_choices validation

This commit is contained in:
Werner Pieterson 2016-11-29 11:07:43 +02:00 committed by Drew Hubl
parent c30c4baed7
commit 6c64640978
3 changed files with 11 additions and 2 deletions

View file

@ -22,7 +22,7 @@ from django.db import models
from django.utils.text import capfirst from django.utils.text import capfirst
from django.core import exceptions from django.core import exceptions
from ..forms.fields import MultiSelectFormField, MaxChoicesValidator from ..forms.fields import MultiSelectFormField, MinChoicesValidator, MaxChoicesValidator
from ..utils import get_max_length from ..utils import get_max_length
from ..validators import MaxValueMultiFieldValidator from ..validators import MaxValueMultiFieldValidator
@ -54,6 +54,8 @@ class MultiSelectField(models.CharField):
super(MultiSelectField, self).__init__(*args, **kwargs) super(MultiSelectField, self).__init__(*args, **kwargs)
self.max_length = get_max_length(self.choices, self.max_length) self.max_length = get_max_length(self.choices, self.max_length)
self.validators[0] = MaxValueMultiFieldValidator(self.max_length) self.validators[0] = MaxValueMultiFieldValidator(self.max_length)
if self.min_choices is not None:
self.validators.append(MinChoicesValidator(self.min_choices))
if self.max_choices is not None: if self.max_choices is not None:
self.validators.append(MaxChoicesValidator(self.max_choices)) self.validators.append(MaxChoicesValidator(self.max_choices))

View file

@ -17,13 +17,14 @@
from django import forms from django import forms
from ..utils import get_max_length from ..utils import get_max_length
from ..validators import MaxValueMultiFieldValidator, MaxChoicesValidator from ..validators import MaxValueMultiFieldValidator, MinChoicesValidator, MaxChoicesValidator
class MultiSelectFormField(forms.MultipleChoiceField): class MultiSelectFormField(forms.MultipleChoiceField):
widget = forms.CheckboxSelectMultiple widget = forms.CheckboxSelectMultiple
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.min_choices = kwargs.pop('min_choices', None)
self.max_choices = kwargs.pop('max_choices', None) self.max_choices = kwargs.pop('max_choices', None)
self.max_length = kwargs.pop('max_length', None) self.max_length = kwargs.pop('max_length', None)
super(MultiSelectFormField, self).__init__(*args, **kwargs) super(MultiSelectFormField, self).__init__(*args, **kwargs)
@ -31,3 +32,5 @@ class MultiSelectFormField(forms.MultipleChoiceField):
self.validators.append(MaxValueMultiFieldValidator(self.max_length)) self.validators.append(MaxValueMultiFieldValidator(self.max_length))
if self.max_choices is not None: if self.max_choices is not None:
self.validators.append(MaxChoicesValidator(self.max_choices)) self.validators.append(MaxChoicesValidator(self.max_choices))
if self.min_choices is not None:
self.validators.append(MinChoicesValidator(self.min_choices))

View file

@ -24,6 +24,10 @@ class MaxValueMultiFieldValidator(validators.MaxLengthValidator):
code = 'max_multifield_value' code = 'max_multifield_value'
class MinChoicesValidator(validators.MinLengthValidator):
message = _(u'You must select a minimum of %(limit_value)d choices.')
code = 'min_choices'
class MaxChoicesValidator(validators.MaxLengthValidator): class MaxChoicesValidator(validators.MaxLengthValidator):
message = _(u'You must select a maximum of %(limit_value)d choices.') message = _(u'You must select a maximum of %(limit_value)d choices.')
code = 'max_choices' code = 'max_choices'