mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Add label to forms, views and templates
This commit is contained in:
@@ -919,21 +919,26 @@ class ComponentCreateView(GetReturnURLMixin, View):
|
||||
new_components = []
|
||||
data = deepcopy(request.POST)
|
||||
|
||||
for i, name in enumerate(form.cleaned_data['name_pattern']):
|
||||
|
||||
names = form.cleaned_data['name_pattern']
|
||||
labels = form.cleaned_data.get('label_pattern')
|
||||
for pos, name in enumerate(names):
|
||||
label = labels[pos] if labels else None
|
||||
# Initialize the individual component form
|
||||
data['name'] = name
|
||||
data['label'] = label
|
||||
if hasattr(form, 'get_iterative_data'):
|
||||
data.update(form.get_iterative_data(i))
|
||||
data.update(form.get_iterative_data(pos))
|
||||
component_form = self.model_form(data)
|
||||
|
||||
if component_form.is_valid():
|
||||
new_components.append(component_form)
|
||||
else:
|
||||
for field, errors in component_form.errors.as_data().items():
|
||||
# Assign errors on the child form's name field to name_pattern on the parent form
|
||||
# Assign errors on the child form's name/label field to name_pattern/label_pattern on the parent form
|
||||
if field == 'name':
|
||||
field = 'name_pattern'
|
||||
if field == 'label':
|
||||
field = 'label_pattern'
|
||||
for e in errors:
|
||||
form.add_error(field, '{}: {}'.format(name, ', '.join(e)))
|
||||
|
||||
@@ -1003,10 +1008,14 @@ class BulkComponentCreateView(GetReturnURLMixin, View):
|
||||
for obj in data['pk']:
|
||||
|
||||
names = data['name_pattern']
|
||||
for name in names:
|
||||
labels = data['label_pattern']
|
||||
for pos, name in enumerate(names):
|
||||
label = labels[pos] if labels else None
|
||||
|
||||
component_data = {
|
||||
self.parent_field: obj.pk,
|
||||
'name': name,
|
||||
'label': label
|
||||
}
|
||||
component_data.update(data)
|
||||
component_form = self.model_form(component_data)
|
||||
|
||||
Reference in New Issue
Block a user