1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Move selection field validation from admin form to model

This commit is contained in:
Jeremy Stretch
2020-11-11 15:24:39 -05:00
parent d17bf67131
commit 3d6baeaab0
2 changed files with 7 additions and 9 deletions

View File

@ -89,14 +89,6 @@ class CustomFieldForm(forms.ModelForm):
order_content_types(self.fields['content_types']) order_content_types(self.fields['content_types'])
def clean(self):
# Validate selection choices
if self.cleaned_data['type'] == CustomFieldTypeChoices.TYPE_SELECT and len(self.cleaned_data['choices']) < 2:
raise forms.ValidationError({
'choices': 'Selection fields must specify at least two choices.'
})
@admin.register(CustomField) @admin.register(CustomField)
class CustomFieldAdmin(admin.ModelAdmin): class CustomFieldAdmin(admin.ModelAdmin):

View File

@ -170,7 +170,13 @@ class CustomField(models.Model):
# Choices can be set only on selection fields # Choices can be set only on selection fields
if self.choices and self.type != CustomFieldTypeChoices.TYPE_SELECT: if self.choices and self.type != CustomFieldTypeChoices.TYPE_SELECT:
raise ValidationError({ raise ValidationError({
'choices': "Choices may be set only for selection-type custom fields." 'choices': "Choices may be set only for custom selection fields."
})
# A selection field must have at least two choices defined
if self.type == CustomFieldTypeChoices.TYPE_SELECT and len(self.choices) < 2:
raise ValidationError({
'choices': "Selection fields must specify at least two choices."
}) })
# A selection field's default (if any) must be present in its available choices # A selection field's default (if any) must be present in its available choices