custom fields: add option which forces user to make an active choice.

in forms.py we prepend a 0 entry to the list of choices.
This commit is contained in:
Andreas Kotowicz 2011-12-01 20:48:37 +01:00
parent 87a5aa88b4
commit 71d69278bc
2 changed files with 16 additions and 5 deletions

View File

@ -58,7 +58,10 @@ class EditTicketForm(forms.ModelForm):
instanceargs['max_digits'] = field.max_length instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list': elif field.data_type == 'list':
fieldclass = forms.ChoiceField 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': elif field.data_type == 'boolean':
fieldclass = forms.BooleanField fieldclass = forms.BooleanField
elif field.data_type == 'date': elif field.data_type == 'date':
@ -192,7 +195,10 @@ class TicketForm(forms.Form):
instanceargs['max_digits'] = field.max_length instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list': elif field.data_type == 'list':
fieldclass = forms.ChoiceField 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': elif field.data_type == 'boolean':
fieldclass = forms.BooleanField fieldclass = forms.BooleanField
elif field.data_type == 'date': elif field.data_type == 'date':
@ -405,9 +411,9 @@ class PublicTicketForm(forms.Form):
instanceargs['max_digits'] = field.max_length instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list': elif field.data_type == 'list':
fieldclass = forms.ChoiceField fieldclass = forms.ChoiceField
choices = [] if field.empty_selection_list:
for line in field.list_values.split("\n"): choices = field.choices_as_array
choices.append((line, line)) choices.insert(0, ('','---------' ) )
instanceargs['choices'] = choices instanceargs['choices'] = choices
elif field.data_type == 'boolean': elif field.data_type == 'boolean':
fieldclass = forms.BooleanField fieldclass = forms.BooleanField

View File

@ -1176,6 +1176,11 @@ class CustomField(models.Model):
null=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 = models.TextField(
_('List Values'), _('List Values'),
help_text=_('For list fields only. Enter one option per line.'), help_text=_('For list fields only. Enter one option per line.'),