Skip to content

Commit

Permalink
custom fields: add option which forces user to make an active choice.
Browse files Browse the repository at this point in the history
in forms.py we prepend a 0 entry to the list of choices.
  • Loading branch information
kotowicz committed Dec 1, 2011
1 parent 87a5aa8 commit 71d6927
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 11 additions & 5 deletions helpdesk/forms.py
Expand Up @@ -58,7 +58,10 @@ def __init__(self, *args, **kwargs):
instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list':
fieldclass = forms.ChoiceField
instanceargs['choices'] = field.choices_as_array
if field.empty_selection_list:
choices = field.choices_as_array
choices.insert(0, ('','---------' ) )
instanceargs['choices'] = choices
elif field.data_type == 'boolean':
fieldclass = forms.BooleanField
elif field.data_type == 'date':
Expand Down Expand Up @@ -192,7 +195,10 @@ def __init__(self, *args, **kwargs):
instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list':
fieldclass = forms.ChoiceField
instanceargs['choices'] = field.choices_as_array
if field.empty_selection_list:
choices = field.choices_as_array
choices.insert(0, ('','---------' ) )
instanceargs['choices'] = choices
elif field.data_type == 'boolean':
fieldclass = forms.BooleanField
elif field.data_type == 'date':
Expand Down Expand Up @@ -405,9 +411,9 @@ def __init__(self, *args, **kwargs):
instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list':
fieldclass = forms.ChoiceField
choices = []
for line in field.list_values.split("\n"):
choices.append((line, line))
if field.empty_selection_list:
choices = field.choices_as_array
choices.insert(0, ('','---------' ) )
instanceargs['choices'] = choices
elif field.data_type == 'boolean':
fieldclass = forms.BooleanField
Expand Down
5 changes: 5 additions & 0 deletions helpdesk/models.py
Expand Up @@ -1175,6 +1175,11 @@ class CustomField(models.Model):
blank=True,
null=True,
)

empty_selection_list = models.BooleanField(
_('Add empty first choice to List?'),
help_text=_('Only for List: adds an empty first entry to the choices list, which enforces that the user makes an active choice.'),
)

list_values = models.TextField(
_('List Values'),
Expand Down

0 comments on commit 71d6927

Please sign in to comment.