Dijit maxLength通过dojo约束语言



我正在进行一个项目,试图将约束映射到XSLT到HTML中的Dijit控件上。

一个这样的约束是文本字段的最大长度。但是,我在Dojo约束语言中找不到这样的约束。

我在下面的例子中遗漏了这样一个约束(不包括RegEx约束)吗?

    <label for="shortField">shortField </label>
    <input id="shortField" data-dojo-type="dijit/form/ValidationTextBox"
        data-dojo-props = "name: 'shortField',
                           width:400,
                           height:150,
                           value: '1234567890',
                           required: true,
                           constraints: { max:10, maxLength:10, length:10, size:10 }
        " />

为了一致性,我更喜欢使用约束,而不是设置_TextBoxMixin的maxLength属性。

(不幸的是,在约束中设置正则表达式也是不可取的,因为这可能会在以后被覆盖,超出我的控制范围。)

似乎不存在这样的约束。此外,由于我发现我还需要验证最小长度,我选择了将Dijit.Form.ValidationTextBox.子类化的解决方案

这是我为isValid方法选择的实现,它从约束中读取,因此我可以保持生成XSL代码的一致性。)

    isValid : function(){
                var ancestorsValid = this.inherited(arguments);
                if(ancestorsValid){
                    //Acquire only meaningful validation boundaries.
                    var minLength = this.constraints && this.constraints.minLength ? Number(this.constraints.minLength) : null;
                    var maxLength = this.constraints && this.constraints.maxLength ? Number(this.constraints.maxLength) : null;
                    //Validate min and max if present.
                    return  ((minLength === null) || this.value.length >= minLength) &&
                            ((maxLength === null) || this.value.length <= maxLength);
                }
                return false;
            }

相关内容

最新更新