Skip to content

Commit

Permalink
fix labels formset
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Jul 22, 2024
1 parent a4178ff commit afa7a26
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
56 changes: 34 additions & 22 deletions lemarche/templates/dashboard/siae_edit_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ <h3 class="fr-callout__title fr-text--sm"><span class="fr-icon-lightbulb-line" a
<div class="fr-grid-row fr-grid-row--gutters fr-mb-4v">
<div class="fr-col-12 fr-col-lg-8">
{% include "includes/forms/_dsfr_formset.html" with formset_title="Labels et certifications" formset=label_formset %}
<div id="labels_old-formset-empty-form" class="fr-hidden">
<div class="formset-row-labels_old formset">
{% dsfr_form label_formset.empty_form %}
</div>
</div>
<button id="labels_old-formset-add-more"
type="button"
class="fr-btn fr-btn--secondary fr-fi-add-circle-line fr-btn--icon-right fr-mt-1w">
Ajouter un label
</button>
</div>
<div class="fr-col-12 fr-col-lg-4">
<div class="fr-callout fr-p-4v">
Expand Down Expand Up @@ -230,28 +240,30 @@ <h3 class="fr-callout__title fr-text--sm"><span class="fr-icon-lightbulb-line" a
});
</script>
<script type="text/javascript">
/**
* Add formset items dynamically
*/
const prefixRegex = new RegExp('__prefix__', 'g');

// label formset
const labelIdString = 'id_labels';
const labelFormsetString = 'label-formset';
const totalLabelFormset = document.getElementById(`${labelIdString}-TOTAL_FORMS`);
const labelFormsetEmptyForm = document.getElementById(`${labelFormsetString}-empty-form`);
const labelFormsetAddMoreButton = document.getElementById(`${labelFormsetString}-add-more`);

labelFormsetAddMoreButton.addEventListener('click', function(e) {
if (e) { e.preventDefault(); }
// add new form
const copyLabelFormsetEmptyForm = labelFormsetEmptyForm.cloneNode(true);
copyLabelFormsetEmptyForm.innerHTML = copyLabelFormsetEmptyForm.innerHTML.replace(prefixRegex, totalLabelFormset.value);
copyLabelFormsetEmptyForm.childNodes.forEach(node => {
labelFormsetEmptyForm.parentNode.insertBefore(node, labelFormsetEmptyForm);
/**
* Add labels formset items dynamically
*/
const formsetPrefix = 'labels_old';
document.getElementById(`${formsetPrefix}-formset-add-more`).addEventListener('click', function() {
// Get the empty form template
var emptyForm = document.getElementById(`${formsetPrefix}-formset-empty-form`).innerHTML;

// Increment the total forms count
var totalFormsInput = document.getElementById(`id_${formsetPrefix}-TOTAL_FORMS`);
var totalForms = parseInt(totalFormsInput.value);
totalFormsInput.value = totalForms + 1;

// Replace the prefix in the empty form template
var newForm = emptyForm.replace(/__prefix__/g, totalForms);

// Create a new div element and set its innerHTML to the new form
var newFormElement = document.createElement('div');
newFormElement.innerHTML = newForm;
newFormElement.classList.add('row' + ((totalForms % 2) + 1), `formset-row-${formsetPrefix}`, 'formset');

// Insert the new form at the end
var formsetGroup = document.querySelector(`.formset-${formsetPrefix}.formset-group`);
formsetGroup.insertBefore(newFormElement, null);
});
// update total forms count
totalLabelFormset.setAttribute('value', parseInt(totalLabelFormset.getAttribute('value'), 10) + 1);
});
</script>
{% endblock extra_js %}
25 changes: 18 additions & 7 deletions lemarche/templates/includes/forms/_dsfr_formset.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
{% extends "dsfr/formset_base.html" %}
{% block formset_title %}
{% if formset %}
<h3 class="fr-mt-3w">
{% load static dsfr_tags widget_tweaks %}
{% if formset %}
<div class="formset-{{ formset.prefix }} formset-group">
{{ formset.management_form }}
<h3 class="fr-mt-3w">
{{ formset_title }}
</h3>
{% endif %}
{% endblock formset_title %}
</h3>
{% for form in formset %}
<div class="{% cycle 'row1' 'row2' %} formset-row-{{ formset.prefix }} formset">
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{% include "dsfr/form_field_snippets/field_snippet.html" %}
{% endfor %}
</div>
{% endfor %}
</div>
{% endif %}
3 changes: 2 additions & 1 deletion lemarche/www/dashboard_siaes/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.forms.models import inlineformset_factory
from django.utils.html import mark_safe
from django_select2.forms import ModelSelect2MultipleWidget
from dsfr.forms import DsfrBaseForm

from lemarche.networks.models import Network
from lemarche.sectors.models import Sector
Expand Down Expand Up @@ -105,7 +106,7 @@ def save(self, *args, **kwargs):
super().save(*args, **kwargs)


class SiaeEditInfoForm(forms.ModelForm):
class SiaeEditInfoForm(forms.ModelForm, DsfrBaseForm):
class Meta:
model = Siae
fields = [
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/dashboard_siaes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def post(self, request, *args, **kwargs):
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
label_formset = SiaeLabelOldFormSet(self.request.POST, instance=self.object)
label_formset = SiaeLabelOldFormSet(request.POST, instance=self.object)
if form.is_valid() and label_formset.is_valid():
return self.form_valid(form, label_formset)
else:
Expand Down

0 comments on commit afa7a26

Please sign in to comment.