Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I highlight all invalid dijit.form.ValidationTextBoxes on a dijit.form.Form?

what I'm after is a form that when submitted, runs a validation check, and highlights all invalid fields and adds the tooltips.

I'm effectively looking for something like this:

dojo.forEach(dijit.byId('myForm')._invalidWidgets, function (thisWidget,index,array) {
    thisWidget.displayMessage("normal invalid/empty message should go here, seems I should be calling something higher level than this");
});

but I don't want to be digging that deep, all I want to do is trigger the same sort of thing that's triggered when you tab out of an empty required field (exclamation icon and appropriate invalid/empty message). Maybe I should just try and fire the tab-out event?

Can someone point me in the right direction?

like image 768
Aaron B. Russell Avatar asked Dec 17 '22 21:12

Aaron B. Russell


1 Answers

Yes, you're correct - you can get all your validation, highlighting, even focus on the first invalid field by just calling the validate() function on a dijit.form.Form element.

Here's an example where the validate() call is added to the onSubmit event:

<head>
    <script type="text/javascript">
        dojo.require("dojo.parser");
        dojo.require("dojo.form.Form");
        dojo.require("dojo.form.ValidationTextBox");
        dojo.require("dojo.form.Button");
        // more includes here...
    </script>
</head>
<body>
    <form dojoType="dijit.form.Form" action="..." method="...">
        <input dojoType="dijit.form.ValidationTextBox" trim="true" regExp="..." invalidMessage="Oops...">
        <!-- // more form elemts here... -->
        <button type="submit" dojoType="dijit.form.Button" ...>
            Submit
        </button>
        <script type="dojo/method" event="onSubmit">
            if (!this.validate()) {
                alert("Form contains invalid data.  Please correct....");
                return false;
            }
            return true;
        <script>
    </form>
</body>

Hope, you find it helpful.

Cheers.


Follow-up: Here's an example of an input field that could be used to help prompt a user as to what sort of data is expected, and would alert them when validation fails:

<input type="text" id="EXT" name="EXT" value=""
    maxLength="10"
    dojoType="dijit.form.ValidationTextBox"
    regExp="\d+?"
    trim="true"
    promptMessage="<p class='help'>Please your extension. (i.e. "1234")</p>"
    invalidMessage="<p class='help'>The extension field should contain only numbers.</p>">

This is a declarative example. (I misspelled it in my initial response below.)

like image 78
S.Jones Avatar answered Mar 09 '23 01:03

S.Jones