简体   繁体   English

如何在dijit.form.Form上突出显示所有无效的dijit.form.ValidationTextBox?

[英]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? 有人能指出我正确的方向吗?

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. 是的,你是对的 - 你可以通过调用dijit.form.Form元素上的validate()函数来获得所有验证,突出显示,甚至专注于第一个无效字段。

Here's an example where the validate() call is added to the onSubmit event: 这是一个将validate()调用添加到onSubmit事件的示例:

<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.) (我在下面的初步回复中拼错了。)

jthomas_ in #dojo on irc.freenode.net answered my question. 在irc.freenode.net的#dojo中的jthomas_回答了我的问题。 It turns out that I wanted to be using dijit.byId('myForm').validate() which does everything I wanted in one swoop. 事实证明,我想使用dijit.byId('myForm').validate()可以一举完成我想要的一切。 Thanks, jthomas_! 谢谢,jthomas_!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM