diff --git a/assets/controllers/bo/form_collection_controller.js b/assets/controllers/bo/form_collection_controller.js index e7a7d4c..cd5742b 100644 --- a/assets/controllers/bo/form_collection_controller.js +++ b/assets/controllers/bo/form_collection_controller.js @@ -8,6 +8,29 @@ export default class extends Controller { this.index = this.collectionTarget.children.length; this._setupDrag(); this._syncOrdering(); + + if (this.index === 0) { + this.addItem(); + } + + this.collectionTarget.addEventListener('input', (e) => { + if (e.target.type !== 'text') return; + const item = e.target.closest('[data-collection-item]'); + const last = [...this.collectionTarget.children].at(-1); + if (item && item === last && e.target.value.trim() !== '') { + this.addItem(); + } + }); + + const form = this.element.closest('form'); + if (form) { + form.addEventListener('submit', () => { + [...this.collectionTarget.children].forEach(item => { + const input = item.querySelector('input[type="text"]'); + if (input && input.value.trim() === '') item.remove(); + }); + }); + } } addItem() { diff --git a/templates/backoffice/partials/answer_row.html.twig b/templates/backoffice/partials/answer_row.html.twig index a3b633e..73d24d6 100644 --- a/templates/backoffice/partials/answer_row.html.twig +++ b/templates/backoffice/partials/answer_row.html.twig @@ -4,13 +4,13 @@
{{ form_widget(answerForm.text) }}
{{ form_widget(answerForm.isRightAnswer) }}
- - {% endmacro %} diff --git a/templates/backoffice/question_bank/_form_body.html.twig b/templates/backoffice/question_bank/_form_body.html.twig index 40b6f96..0696966 100644 --- a/templates/backoffice/question_bank/_form_body.html.twig +++ b/templates/backoffice/question_bank/_form_body.html.twig @@ -1,6 +1,6 @@ {% import 'backoffice/partials/answer_row.html.twig' as macros %} -{{ form_start(form) }} +{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }} {{ form_row(form.question) }} {{ form_row(form.reusable) }} {{ form_row(form.labels) }} diff --git a/templates/backoffice/quiz/_question_form_body.html.twig b/templates/backoffice/quiz/_question_form_body.html.twig index 0722a35..701d7f2 100644 --- a/templates/backoffice/quiz/_question_form_body.html.twig +++ b/templates/backoffice/quiz/_question_form_body.html.twig @@ -1,6 +1,6 @@ {% import 'backoffice/partials/answer_row.html.twig' as macros %} -{{ form_start(form) }} +{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }} {{ form_row(form.question) }}
{% endif %} - -