diff --git a/assets/controllers/form_controller.js b/assets/controllers/form_controller.js new file mode 100644 index 0000000..0fc5078 --- /dev/null +++ b/assets/controllers/form_controller.js @@ -0,0 +1,33 @@ +import {Controller} from "@hotwired/stimulus"; + + +export default class extends Controller { + static targets = ['ticket', 'afterShowTicket', 'kid12yo', 'kid6yo', 'meat', 'vegetarian', 'vegan', 'error'] + + connect() { + this.element.addEventListener('submit', (e) => this.submit(e)) + } + + submit(e) { + e.preventDefault(); + const ticketCount = this.getTotalTicketCount(); + const foodCount = this.getTotalFoodCount(); + + if (foodCount > ticketCount) { + this.errorTarget.textContent = 'Sie können nicht mehr Essen als Tickets bestellen'; + } + } + + getTotalTicketCount() { + return parseInt(this.ticketTarget.value) + + parseInt(this.kid12yoTarget.value) + + parseInt(this.kid6yoTarget.value) + + parseInt(this.afterShowTicketTarget.value); + } + + getTotalFoodCount() { + return parseInt(this.meatTarget.value) + + parseInt(this.vegetarianTarget.value) + + parseInt(this.veganTarget.value); + } +} \ No newline at end of file diff --git a/templates/ticket/index.html.twig b/templates/ticket/index.html.twig index cd0ba9e..7f53ea6 100644 --- a/templates/ticket/index.html.twig +++ b/templates/ticket/index.html.twig @@ -20,30 +20,57 @@