Validating question type (input) fields with JavaScript, which are created with JavaScript in moodle 2.1

Validating question type (input) fields with JavaScript, which are created with JavaScript in moodle 2.1

by Michael Schink -
Number of replies: 4

Hello, my actual problem is that I got an self-developed question type, which I upgraded for moodle 2.1 lately. The input fields of this question type are generated with JavaScript/JQuery and also validated with JQuery. Validation still works in moodle 2.1, but when a wrong answer/input is made and valdiation is fired the "Next"-button (, which brings you to the next page of an quiz) is disabled and isn't working correctly too.

I spent a lot of hours trying to solve and to explore this problem. Actually I can only say that moodle code is processed before the validation stops it, after the button is clicked. The "Next"-button gets two new attributes, before validation stops processing: id (e.g.: id=yui_3_2_0_1_1319054073190298) and disabled="". If I enable the "Next"-button or/and delete the id the button still doesn't perform the submit. I tried to change the validation and to execute the validation directly (, without using $PAGE->requires->js_init_code("JS Code");) and a lot of other things, but the problem is still the same.

Has anybody have had such a problem or got a clue how to solve this? Are there any other ways to do a validation? Would the problem be solved if I use YUI3 form module for validation and is it possible to use this validation on forms that aren't created with YUI3 (, because it would be a lot of work to recode the whole "design" of the question type with YUI3 instead of JQuery) ? I would appreciate any ideas.

Average of ratings: -
In reply to Michael Schink

Re: Validating question type (input) fields with JavaScript, which are created with JavaScript in moodle 2.1

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you are focussing your attention on the Next button then you are going to sturuggle, because that is just one of many ways that the form can get submitted. Remember that questions can appear in the question preview window as well as in the quiz. And note that the quiz navigation will submit the quiz form via JavaScript when you click on one of the links there.

So, you need to focus on the form submit event. Is there a way to interrupt the procedure if another bit of JS does formnode.submit(); ?

It would be interesting to know what this question type is. If I knew what you were trying to achieve (from an educational point-of-view) I might be better able to advise.

I can't help feeling that trying to block the form submission is obnoxious behaviour for a question type. For example, there may be mulitple other questions on the page. What gives your question type the right to say that they studnet is not allowed to save their current response to those other questions, or navigate to another page of the quiz now? Also, what happens if the time-limit runs out, and so the quiz must be submitted now so the student can get partial credit for what they have done.

Basically, question must be designed so that they can be safely embedded in any other host page. So, like any guest, question must act politely and respectfully considering what they bring into the host's home.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Validating question type (input) fields with JavaScript, which are created with JavaScript in moodle 2.1

by Michael Schink -

The question type is used for accounting questions. About a months ago you guided me through the upgrade from 1.9 to 2.1.

You are right I just focused on the "Next"-button and didn't see that the question navigation also bypasses the validation. The validation is fired when a student adds a new "line" to define an accounting and doesn't enter a digit, because a new accounting is always added with an empty input field (, and two select fields for account number and class on each side).

The easiest way to solve this problem seems to be to delete the validation and to add 0 when adding an new accounting. I just tested this scenario and didn't get an error. Even if i add no value to the input field the evaluation/review works correctly.

From an educational point-of-view the validation should just remind the student that a new accounting was added, but no value was entered. Due to your explanations I think it would be a lot of work to implement this more or less senseless validation correctly. I also think it could easily produce errors, when using questions of this qt out of a quiz or on further moodle updates.

So again,  thank you for your quick help and your explanations. You made my day big grin.

In reply to Michael Schink

Re: Validating question type (input) fields with JavaScript, which are created with JavaScript in moodle 2.1

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, you could go for a more un-obtrusive form of validation, for example just adding a validation message like 'warning, you must fill in a value here' in red somewhere near the blank field. You can do validation onkeydown, or onfocus/onblur, rather than waiting for the submit.

It seems sensible to help the student avoid certain trivial mistakes, but since it is in their own interest to take notice of any warnings, I think we don't need to actually enforce them, and prevent submission of the quiz forum when an error is visbile.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Validating question type (input) fields with JavaScript, which are created with JavaScript in moodle 2.1

by Michael Schink -

That's a good idea. Each question has a hint that you should fill in a value. I just deactivated the onsubmit validation and now I'm gonna extend the is_complete_response(array $response) method to "replace" the onsubmit validation. So the student sees that a question answer with no value isn't answered in the quiz summery. That should be the best way to handle this problem. Thanks again.