mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Merge pull request #4142 from netbox-community/3766-enable_widgets_in_script_fields
Fixes: #3766 - Enable widget usage in scripts
This commit is contained in:
@@ -48,7 +48,7 @@ class ScriptVariable:
|
|||||||
"""
|
"""
|
||||||
form_field = forms.CharField
|
form_field = forms.CharField
|
||||||
|
|
||||||
def __init__(self, label='', description='', default=None, required=True):
|
def __init__(self, label='', description='', default=None, required=True, widget=None):
|
||||||
|
|
||||||
# Initialize field attributes
|
# Initialize field attributes
|
||||||
if not hasattr(self, 'field_attrs'):
|
if not hasattr(self, 'field_attrs'):
|
||||||
@@ -59,6 +59,8 @@ class ScriptVariable:
|
|||||||
self.field_attrs['help_text'] = description
|
self.field_attrs['help_text'] = description
|
||||||
if default:
|
if default:
|
||||||
self.field_attrs['initial'] = default
|
self.field_attrs['initial'] = default
|
||||||
|
if widget:
|
||||||
|
self.field_attrs['widget'] = widget
|
||||||
self.field_attrs['required'] = required
|
self.field_attrs['required'] = required
|
||||||
|
|
||||||
# Initialize the list of optional validators if none have already been defined
|
# Initialize the list of optional validators if none have already been defined
|
||||||
@@ -71,6 +73,9 @@ class ScriptVariable:
|
|||||||
"""
|
"""
|
||||||
form_field = self.form_field(**self.field_attrs)
|
form_field = self.form_field(**self.field_attrs)
|
||||||
if not isinstance(form_field.widget, forms.CheckboxInput):
|
if not isinstance(form_field.widget, forms.CheckboxInput):
|
||||||
|
if form_field.widget.attrs and 'class' in form_field.widget.attrs.keys():
|
||||||
|
form_field.widget.attrs['class'] += ' form-control'
|
||||||
|
else:
|
||||||
form_field.widget.attrs['class'] = 'form-control'
|
form_field.widget.attrs['class'] = 'form-control'
|
||||||
|
|
||||||
return form_field
|
return form_field
|
||||||
|
Reference in New Issue
Block a user