forked from OCA/event
-
Notifications
You must be signed in to change notification settings - Fork 0
Ensure at least one checkbox is checked in mandatory multiple questions #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
remi-filament
merged 14 commits into
16.0-add-website_event_questions_multiple
from
16.0-mandatory-multiple-questions
Mar 11, 2025
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3abdd87
wip mandatory multiple choice
Hugo-Trentesaux 15afdaa
server-side validation for mandatory multiple questions
Hugo-Trentesaux cb3fe1e
improve readability
Hugo-Trentesaux 56573fb
wip PoC for multi checkbox js validation
Hugo-Trentesaux dacf49a
improve user feedback
Hugo-Trentesaux a71efed
pre-commit
Hugo-Trentesaux 621b08a
license
Hugo-Trentesaux 65beb69
[FIX] pre-commit
remi-filament 468882e
[UPD] manifest
remi-filament 1482567
[MOV] rename js to esm.js
remi-filament 4cbcae4
[FIX] apply eslint
Hugo-Trentesaux f860318
wip
Hugo-Trentesaux cd2cdb2
[FIX] remove bootstrap Modal explicit import
Hugo-Trentesaux df26d41
[FIX] get full line clickable
remi-filament File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Translation of Odoo Server. | ||
| # This file contains the translation of the following modules: | ||
| # * website_event_questions_multiple | ||
| # | ||
| msgid "" | ||
| msgstr "" | ||
| "Project-Id-Version: Odoo Server 16.0\n" | ||
| "Report-Msgid-Bugs-To: \n" | ||
| "POT-Creation-Date: 2025-03-06 11:52+0000\n" | ||
| "PO-Revision-Date: 2025-03-06 11:52+0000\n" | ||
| "Last-Translator: \n" | ||
| "Language-Team: \n" | ||
| "MIME-Version: 1.0\n" | ||
| "Content-Type: text/plain; charset=UTF-8\n" | ||
| "Content-Transfer-Encoding: \n" | ||
| "Plural-Forms: \n" | ||
|
|
||
| #. module: website_event_questions_multiple | ||
| #: model:ir.model,name:website_event_questions_multiple.model_event_question | ||
| msgid "Event Question" | ||
| msgstr "Question" | ||
|
|
||
| #. module: website_event_questions_multiple | ||
| #: model:ir.model.fields.selection,name:website_event_questions_multiple.selection__event_question__question_type__multiple_choice | ||
| msgid "Multiple Selection" | ||
| msgstr "Sélection multiple" | ||
|
|
||
| #. module: website_event_questions_multiple | ||
| #: model:ir.model.fields,field_description:website_event_questions_multiple.field_event_question__question_type | ||
| msgid "Question Type" | ||
| msgstr "Type de question" | ||
|
|
||
| #. module: website_event_questions_multiple | ||
| #: model_terms:ir.ui.view,arch_db:website_event_questions_multiple.registration_event_question | ||
| msgid "Please select at least one checkbox." | ||
| msgstr "Veuillez cocher au moins une case." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
website_event_questions_multiple/static/src/js/form_validation.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /** @odoo-module **/ | ||
|
|
||
| import publicWidget from "web.public.widget"; | ||
| import EventRegistrationForm from "website_event.website_event"; | ||
|
|
||
| import ajax from "web.ajax"; | ||
|
|
||
| /// declare widget | ||
| // extends from EventRegistrationForm from ocb/addons/website_event/static/src/js/website_event.js | ||
| export const EventRegistrationFormWithValidation = EventRegistrationForm.extend({ | ||
| /// ------------------------------------------------ | ||
| /// WARNING: code duplication for lack of extensibility | ||
| // this is a copy of the on_click function of the parent class | ||
| // which is the one opening the modal with these steps: | ||
| // 1. when clicking on "Register button" | ||
| // 2. look at selection (number of tickets to register) | ||
| // 3. disable the register button if no valid answer (e.g. zero tickets) | ||
| // 4. make an ajax.jsonRpc call to fetch the modal | ||
| // 5. inject the modal inside the page (there can be multiple instances of the modal if the register button is clicked multiple times) | ||
| // the only added behavior is to return the modal class to be able to add form validation logic | ||
| // return value is a Promise<modal | undefined> | ||
| on_click_parent: async function (ev) { | ||
| ev.preventDefault(); | ||
| ev.stopPropagation(); | ||
| var $form = $(ev.currentTarget).closest("form"); | ||
| var $button = $(ev.currentTarget).closest('[type="submit"]'); | ||
| var post = {}; | ||
| $("#registration_form table").siblings(".alert").remove(); | ||
| $("#registration_form select").each(function () { | ||
| post[$(this).attr("name")] = $(this).val(); | ||
| }); | ||
| var tickets_ordered = _.some( | ||
| _.map(post, function (value, key) { | ||
| return parseInt(value); | ||
| }) | ||
| ); | ||
| if (!tickets_ordered) { | ||
| $('<div class="alert alert-info"/>') | ||
| .text(_t("Please select at least one ticket.")) | ||
| .insertAfter("#registration_form table"); | ||
| return new Promise(function () {}); | ||
| } else { | ||
| $button.attr("disabled", true); | ||
| var action = $form.data("action") || $form.attr("action"); | ||
| var self = this; | ||
| return ajax.jsonRpc(action, "call", post).then(async function (modal) { | ||
| const tokenObj = await self._recaptcha.getToken( | ||
| "website_event_registration" | ||
| ); | ||
| if (tokenObj.error) { | ||
| self.displayNotification({ | ||
| type: "danger", | ||
| title: _t("Error"), | ||
| message: tokenObj.error, | ||
| sticky: true, | ||
| }); | ||
| $button.prop("disabled", false); | ||
| return false; | ||
| } | ||
| var $modal = $(modal); | ||
| $modal.find(".modal-body > div").removeClass("container"); // retrocompatibility - REMOVE ME in master / saas-19 | ||
| $modal.appendTo(document.body); | ||
| const modalBS = new Modal($modal[0], { | ||
| backdrop: "static", | ||
| keyboard: false, | ||
| }); | ||
| modalBS.show(); | ||
| $modal.appendTo("body").modal("show"); | ||
| $modal.on("click", ".js_goto_event", function () { | ||
| $modal.modal("hide"); | ||
| $button.prop("disabled", false); | ||
| }); | ||
| $modal.on("click", ".btn-close", function () { | ||
| $button.prop("disabled", false); | ||
| }); | ||
| $modal.on("submit", "form", function (ev) { | ||
| const tokenInput = document.createElement("input"); | ||
| tokenInput.setAttribute("name", "recaptcha_token_response"); | ||
| tokenInput.setAttribute("type", "hidden"); | ||
| tokenInput.setAttribute("value", tokenObj.token); | ||
| ev.currentTarget.appendChild(tokenInput); | ||
| }); | ||
| // THIS IS THE ONLY REAL MODIFICATION | ||
| // return the $modal jQuery object | ||
| return $modal; | ||
| }); | ||
| } | ||
| }, | ||
| /// ------------------------------------------------ | ||
|
|
||
| /** | ||
| * @override | ||
| * override the parent method to replace call to the modified function | ||
| */ | ||
| on_click: async function (ev) { | ||
| // get modal from copy (not super()) | ||
| const $modal = await this.on_click_parent(ev); | ||
| if ($modal) { | ||
| this.add_validation($modal); | ||
| } else { | ||
| console.log("No modal was added."); | ||
| } | ||
| }, | ||
|
|
||
| // this is where I add validation to the form in the modal | ||
| add_validation: function ($modal) { | ||
| console.log("Adding validation to modal."); | ||
|
|
||
| // prevent default | ||
| $modal.on("submit", "form", function (ev) { | ||
| console.log("form submitted"); | ||
|
|
||
| // search all check groups with mandatory answers | ||
| $modal | ||
| .find("div.form-check-group.is_mandatory_answer") | ||
| .each(function (index) { | ||
| console.log("testing group", index); | ||
| // count number of checkbox | ||
| let checked_count = 0; | ||
| $(this) | ||
| .find(".form-check-input") | ||
| .each(function () { | ||
| if ($(this).prop("checked")) checked_count++; | ||
| }); | ||
|
|
||
| // if zero, prevent default and display message | ||
| if (checked_count == 0) { | ||
| console.log("at least one checkbox must be checked"); | ||
| $(this).find(".mandatory-message").removeClass("d-none"); | ||
| ev.preventDefault(); | ||
| ev.stopPropagation(); | ||
| } | ||
| }); | ||
| }); | ||
| }, | ||
| }); | ||
|
|
||
| /// register widget | ||
| // (also copied from parent) | ||
| publicWidget.registry.EventRegistrationFormWithValidationInstance = | ||
| publicWidget.Widget.extend({ | ||
| selector: "#registration_form", | ||
|
|
||
| /** | ||
| * @override | ||
| */ | ||
| start: function () { | ||
| console.log("instance start override"); | ||
| var def = this._super.apply(this, arguments); | ||
| this.instance = new EventRegistrationFormWithValidation(this); // <--- here we instantiante child widget | ||
| return Promise.all([def, this.instance.attachTo(this.$el)]); | ||
| }, | ||
| /** | ||
| * @override | ||
| */ | ||
| destroy: function () { | ||
| this.instance.setElement(null); | ||
| this._super.apply(this, arguments); | ||
| this.instance.setElement(this.$el); | ||
| }, | ||
| }); | ||
|
|
||
| console.log("form validation widget registered"); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.