Select to view content in your preferred language

Unable to get form to validate when I click submit button.

6610
7
04-29-2015 01:49 PM
ChrisSergent
Deactivated User

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!");

        }

      

       

    }

0 Kudos
7 Replies
KenBuja
MVP Esteemed Contributor

This message means that the object 'document.getElementById("#feedbackModal")' is null. Should you use "feedbackModal" instead of "#feedbackModal"?

ChrisSergent
Deactivated User

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.

0 Kudos
ChrisSergent
Deactivated User

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.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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; 
}));
ChrisSergent
Deactivated User

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.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Chris,

   So the reason is that the scope of "this" is NOT the form. What is the id of your from?

0 Kudos
ChrisSergent
Deactivated User

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.

0 Kudos