I have three fields in a form call feedbackModal. They are all required and mark withed required, but when I click submit, I receive the following error: Uncaught TypeError: Cannot read property '0' of null for the following lines of code:
// submit or cancel request and hide modal
query("#feedbackModal .btn").on("click", function (e) { // e is defined but never used.
// NOTE: this is not implemented in sample app
if (document.getElementById("#feedbackModal")[0].checkValidity()) {
query("#feedbackModal").modal("hide");
}
else {
//show errors
return false;
}
});
and this one:
on(dom.byId("btnFeedback"), "click", function () {
sendEmail();
});
function sendEmail() {
if (document.getElementById("#feedbackModal")[0].checkValidity()) {
var link = "mailto:" + document.getElementById("eMail").value
+ "&subject=" + escape(document.getElementById('subject').value)
+ "&body=" + escape(document.getElementById('comment').value)
;
}
else {
//show errors
return false;
alert("All fields are required!");
}
}
This message means that the object 'document.getElementById("#feedbackModal")' is null. Should you use "feedbackModal" instead of "#feedbackModal"?
I actually don't think it will. I realized that I am trying to use jQuery syntax. Tom Wayson said that I should try dojo instead: https://dojotoolkit.org/documentation/tutorials/1.10/validation/ I will update the post if it works or does not.
I have this block of code and I still get: Uncaught TypeError: Cannot read property 'validate' of undefined
on(dom.byId("btnFeedback"), "click", function () { sendEmail(); }); function sendEmail(ev) { if (this.validate()) { return confirm('Form is valid, press OK to submit'); } else { alert('Form contains invalid data. Please correct first'); return false; } return true; }
I added the required statements.
Chris,
Sounds like a great time to start learning to use lang.hitch as this sounds like a scope issue.
on(dom.byId("btnFeedback"), "click", lang.hitch(this, function () { if (this.validate()) { return confirm('Form is valid, press OK to submit'); } else { alert('Form contains invalid data. Please correct first'); return false; } return true; }));
I added it and got the following error:
Uncaught TypeError: this.validate is not a function
I also included my require statements for dojo/_base/lang and the variable.
Chris,
So the reason is that the scope of "this" is NOT the form. What is the id of your from?
It feedbackModal.
I changed the code to:
(dom.byId("feedbackModal").validate())
and just used feedbackModal with and without quotes and tried prefixing with a # sign. Still the same error message.