Dojo / Dijit设置无效消息和验证失败 [英] Dojo/Dijit setting invalid message and failing validation

查看:125
本文介绍了Dojo / Dijit设置无效消息和验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为该项目定义了一个具有 missingMessage invalidMessage 的表单项。我将该表单验证方法称为验证。如果项目为空,则调用 missingMessage ,并显示红色感叹号错误图标。

I have a form item with missingMessage and invalidMessage defined for the item. I call the form validation method on the item for validation. If the item is empty the missingMessage is called and the red exclamation mark error icon is shown.

当用户输入特定值时,我正在设置文本项上的 invalidMessage 如何获取 invalidMessage 和红色感叹号显示没有硬编码对文本项的约束。

I am trying to set the invalidMessage on text item when the user enters a specific value how can i get the invalidMessage and the red exclamation mark to show without hard coding a constraint on the text item.

我想使用这个对项目进行自定义验证。如果用户输入特定值,我想失败验证并返回错误。 这里我试图告诉用户他们不能输入值'John'在名字字段。我正在得到红色感叹号,但是 invalidMessage 没有显示。

I would like to use this for custom validation on items. If the user enters a particular value i would like fail the validation and return an error. Here i am trying to tell the user they cannot enter the value 'John' in the first name field. I am getting the red exclamation mark set however the invalidMessage is not showing.

我希望用户点击感叹号可以看到错误消息,当他们开始键入或更改字段的内容时,不再显示感叹号的错误消息。

I would like when the user clicks on the exclamation mark they can see the error message and when they begin to type or change the contents of the field the exclamation mark nad error message is no longer shown.

Jsp

<body class="claro">
    <form id="myform" data-dojo-type="dijit/form/Form">

    <input
    data-dojo-type="dijit/form/ValidationTextBox"
        data-dojo-props="
            regExp: '[\\w]+',
            required: true,
            invalidMessage: 'Invalid First Name !',
            missingMessage: 'First Name Required !'"
        id="fnameTextBox" title="First Name"
    placeholder="Your First Name"
    />

    <input
    data-dojo-type="dijit/form/ValidationTextBox"
        data-dojo-props="
            regExp: '[\\w]+',
            required: true,
            invalidMessage: 'Invalid Last Name !',
            missingMessage: 'Last Name Required !'"
        id="lnameTextBox" title="Last Name"
    placeholder="Your Last Name"
    />

     <button id="validateFields" data-dojo-type="dijit/form/Button">Validate</button>
    </form>
</body>

Javascript

dojo.require("dijit/form/Form");
dojo.require("dijit/form/Button");
dojo.require("dijit/form/ValidationTextBox");
dojo.require("dijit/Tooltip");

dojo.ready(function() {
    var fName = dijit.byId("fnameTextBox");
    var lName = dijit.byId("lnameTextBox"); 

    dojo.connect(dijit.byId("validateFields"), "onClick", function() {      
        var myform = dijit.byId('myform');
        myform.connectChildren();   


        if(fName == "John"){
            dijit.byId("fnameTextBox")._set("state","Error");
            dijit.byId("fnameTextBox").attr('_ErrorMessage', 'John is not allowed');

        }else{
             myform.validate();
        }

    }); 
});


推荐答案

最佳做法方法为特定小部件提供单独的验证规则覆盖该小部件的默认 validator()方法。

The best practice method to provide separate validation rules for a particular widget is overriding the default validator() method of that widget.

var fnameTextBox = dijit.byId('fnameTextBox');
fnameTextBox.validator = function() {
   if(this.value.indexOf("John")>-1) {
     this.set("invalidMessage",this.value+" is not allowed!");
     return false;
   }
   return true;
}
//below 2 lines to programmatically validate a specific field
fnameTextBox._hasBeenBlurred = true;
fnameTextBox.validate();
//To show the message on failing the field must be focussed
fnameTextBox.focus();

验证器函数可以有自己的逻辑用于验证,并且仅对此字段小部件是主观的。如果同样必须应用于多个,则定义函数而不是匿名,并将其用于相应的字段。

The validator function can have its own logic for validating and is subjective only to this field widget. If same has to be applied to more than one, define the function instead of anonymous, and use it for the respective fields.

但是,如果它只是一个值检查,我们可以给出一个正则表达式表达式。

But, if it only a value check, we can give just a regexp expression for this.

regExp: '^((?!John).)*$'

以下不是最佳做法。因为调用form.validate()仍然会将此字段验证为true,这是不正确的。

The following is not the best practice to do it. Because calling form.validate() will still validate this field as true, which is not correct.

dijit.byId("fnameTextBox")._set("state","Error");
dijit.byId("fnameTextBox").attr('_ErrorMessage', 'John is not allowed');

这篇关于Dojo / Dijit设置无效消息和验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆