{% if type == 'new' %} {{ form_start(form, { 'action': path('new_promotion', { mode: 'produit' }), 'method': 'POST', 'attr': { 'class': 'm-form m-form--fit m-form--label-align-right' } }) }} {% else %} {{ form_start(form, { 'action': path('edit_promotion', { 'id': promotion.id }), 'method': 'POST', 'attr': { 'class': 'm-form m-form--fit m-form--label-align-right' } }) }} {% endif %} {% if type != 'new' and promotion is defined %} {% endif %} {# --- Modale Sélecteur de produits --- #} {{ form_errors(form) }} {{ form_rest(form) }} {{ form_end(form) }} est posé côté Twig pour l'édition #} if (window._promotionSelectedProducts && Array.isArray(window._promotionSelectedProducts)) { window._promotionSelectedProducts.forEach(function(p){ finalSelected[p.id] = p; }); renderFinalSelection(); } else { {# fallback : si des inputs cachés existent (ex: après une validation échouée) #} $('#selectedProductInputs input[type="hidden"]').each(function(){ var id = $(this).val(); if (id) finalSelected[id] = { id:id, ref:'', name:'', imageUrl:null }; }); renderFinalSelection(); } {# (NOUVEAU) RESET / REHYDRATE à l'ouverture de la modale "Promotion" #} $(document).on('shown.bs.modal', '#m_modal_promotion', function () { {# 1) Vider la sélection en mémoire + UI (cas "New" surtout) #} pickerSelected = {}; finalSelected = {}; renderFinalSelection(); {# 2) Vider le SYMFONY À LA SOUMISSION (évite l'erreur Doctrine) #} function getHiddenSelectProduits() { {# id standard généré par Symfony #} var $sel = $('#promotion_produits'); if ($sel.length) return $sel; {# fallback par name (avec ou sans []) #} $sel = $('select[name$="[produits][]"], select[name$="[produits]"]'); return $sel.first(); } function syncHiddenSelectFromFinalSelection() { var $select = getHiddenSelectProduits(); if (!$select.length) return; {# reset sélection #} $select.find('option').prop('selected', false); {# injecter uniquement des IDs numériques uniques #} var seen = {}; Object.values(finalSelected).forEach(function (p) { var id = String(p.id || '').trim(); if (!/^\d+$/.test(id) || seen[id]) return; seen[id] = true; var $opt = $select.find('option[value="' + id + '"]'); if (!$opt.length) { $opt = $('