42 lines
1.8 KiB
Python
42 lines
1.8 KiB
Python
![]() |
# Copyright (c) 2012 by Pablo Martín <goinnn@gmail.com>
|
||
|
#
|
||
|
# This program is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU Lesser General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU Lesser General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU Lesser General Public License
|
||
|
# along with this programe. If not, see <https://www.gnu.org/licenses/>.
|
||
|
|
||
|
from django import forms
|
||
|
|
||
|
|
||
|
from ..forms.widgets import MultiSelectCheckboxSelectMultiple, SortMultiSelectCheckboxSelectMultiple
|
||
|
from ..utils import get_max_length
|
||
|
from ..validators import MaxValueMultiFieldValidator, MinChoicesValidator, MaxChoicesValidator
|
||
|
|
||
|
|
||
|
class MultiSelectFormField(forms.MultipleChoiceField):
|
||
|
widget = MultiSelectCheckboxSelectMultiple
|
||
|
|
||
|
def __init__(self, *args, **kwargs):
|
||
|
self.min_choices = kwargs.pop('min_choices', None)
|
||
|
self.max_choices = kwargs.pop('max_choices', None)
|
||
|
self.max_length = kwargs.pop('max_length', None)
|
||
|
super().__init__(*args, **kwargs)
|
||
|
self.max_length = get_max_length(self.choices, self.max_length)
|
||
|
self.validators.append(MaxValueMultiFieldValidator(self.max_length))
|
||
|
if self.max_choices is not None:
|
||
|
self.validators.append(MaxChoicesValidator(self.max_choices))
|
||
|
if self.min_choices is not None:
|
||
|
self.validators.append(MinChoicesValidator(self.min_choices))
|
||
|
|
||
|
|
||
|
class SortMultiSelectFormField(MultiSelectFormField):
|
||
|
widget = SortMultiSelectCheckboxSelectMultiple
|