Changeset 7283

Show
Ignore:
Timestamp:
02/11/07 18:55:49 (22 months ago)
Author:
doughays
Message:

Add support for user specified regular expressions in ValidationTextbox? to remove the need for 1-off subclasses.

Location:
dijit/trunk
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • dijit/trunk/base/FormElement.js

    r7215 r7283  
    7878                                dojo.html.prependClass(this.domNode, this["class"]+"Disabled"); 
    7979                        } 
    80                         // TODO: if the widget is disabled do we need to do this? 
    81                         this.containerNode.removeAttribute("tabIndex"); 
     80                        // needed for FF when a tabIndex=0 div is inside a Button that is disabled 
     81                        if (this.containerNode){ 
     82                                this.containerNode.removeAttribute("tabIndex"); 
     83                        } 
    8284                }else{ 
    8385                        dojo.html.removeClass(this.domNode, this["class"]+"Disabled"); 
    84                         this.containerNode.setAttribute("tabIndex", this.tabIndex); 
     86                        if (this.containerNode){ 
     87                                this.containerNode.setAttribute("tabIndex", this.tabIndex); 
     88                        } 
    8589                } 
    8690                this.domNode.disabled = this.disabled = disabled; 
    87                 dojo.widget.wai.setAttr(this.domNode, "waiState", "disabled", disabled); 
     91                dijit.util.wai.setAttr(this.domNode, "waiState", "disabled", disabled); 
    8892        }, 
    8993 
     
    9296        }, 
    9397         
     98        postCreate: function(){ 
     99                this._setDisabled(this.disabled == true); 
     100        }, 
     101 
    94102        setValue: function(newValue){ 
    95103                // summary: set the value of the widget. 
  • dijit/trunk/form/templates/Textbox.html

    r7259 r7283  
    11<input dojoAttachPoint='textbox' dojoAttachEvent='onblur;onfocus' 
    2         id='${this.id}' name='${this.name}' class="dojoInputField" type='${this.type}' > 
     2        id='${this.id}' name='${this.name}' class="dojoInputField" type='${this.type}' size='${this.size}' maxlength='${this.maxlength}'> 
  • dijit/trunk/form/templates/ValidationTextbox.html

    r7276 r7283  
    11<span> 
    22        <input dojoAttachPoint='textbox' type='${this.type}' dojoAttachEvent='onblur;onfocus;onkeyup' 
    3                 id='${this.widgetId}' name='${this.name}' size='${this.size}' maxlength='${this.maxlength}' 
    4                 class='${this.className}'> 
    5         <span dojoAttachPoint='invalidSpan' class='${this.invalidClass}'>${this.messages.invalidMessage}</span> 
    6         <span dojoAttachPoint='missingSpan' class='${this.missingClass}'>${this.messages.missingMessage}</span> 
    7         <span dojoAttachPoint='rangeSpan' class='${this.rangeClass}'>${this.messages.rangeMessage}</span> 
     3                id='${this.id}' name='${this.name}' size='${this.size}' maxlength='${this.maxlength}' 
     4                class='dojoInputField'> 
     5        <span dojoAttachPoint='messageSpan' class='dojoValidationTextboxMessage'></span> 
    86</span> 
  • dijit/trunk/form/Textbox.js

    r7258 r7283  
    3232                ucFirst: false, 
    3333 
     34                // size: String 
     35                //              Basic input tag size declaration. 
     36                size: "", 
     37 
     38                // maxlength: String 
     39                //              Basic input tag maxlength declaration. 
     40                maxlength: "", 
     41 
    3442                //      digit: Boolean 
    3543                //              Removes all characters that are not digits if true.  Default is false. 
     
    3947         
    4048                postCreate: function() { 
     49                        dijit.form.Textbox.superclass.postCreate.apply(this); 
    4150                        // assign value programatically in case it has a quote in it 
    4251                        this.textbox.value = this.value; 
  • dijit/trunk/form/ValidationTextbox.js

    r7276 r7283  
    33dojo.require("dijit.form.Textbox"); 
    44dojo.require("dojo.i18n.common"); 
     5dojo.require("dojo.validate.common"); 
    56 
    67dojo.requireLocalization("dijit.form", "validate"); 
     
    910        "dijit.form.ValidationTextbox", 
    1011        dijit.form.Textbox, 
    11         function() { 
     12        { 
    1213                // summary: 
    1314                //              A subclass of Textbox. 
    1415                //              Over-ride isValid in subclasses to perform specific kinds of validation. 
    15                  
    16                 // this property isn't a primitive and needs to be created on a per-item basis. 
    17                 this.flags = {}; 
    18         }, 
    19         { 
     16 
    2017                // default values for new subclass properties 
    2118                // required: Boolean 
    2219                //              Can be true or false, default is false. 
    2320                required: false, 
    24                 // rangeClass: String 
    25                 //              Override default class used for out-of-range input data 
    26                 rangeClass: "range", 
    27                 // invalidClass: String 
    28                 //              Class used to format displayed text in page if necessary to override default class 
    29                 invalidClass: "invalid", 
    30                 // missingClass: String 
    31                 //              Override default class used for missing input data 
    32                 missingClass: "missing", 
    33                 classPrefix: "dojoValidate", 
    34                 // size: String 
    35                 //              Basic input tag size declaration. 
    36                 size: "", 
    37                 // maxlength: String 
    38                 //              Basic input tag maxlength declaration.   
    39                 maxlength: "", 
    4021                // promptMessage: String 
    4122                //              Will not issue invalid message if field is populated with default user-prompt text 
     
    4526                invalidMessage: "", 
    4627                // missingMessage: String 
    47                 //              The message to display if value is missing. 
     28                //              The message to display if value is missing or missing. 
    4829                missingMessage: "", 
     30                // rangeMessage: String 
     31                //              The message to display if value is out-of-range 
    4932                rangeMessage: "", 
    5033                // listenOnKeyPress: Boolean 
    5134                //              Updates messages on each key press.  Default is true. 
    5235                listenOnKeyPress: true, 
     36                // flags: Object 
     37                //              user-defined object needed to pass parameters to the validator functions 
     38                flags: (function(){return {};})(), 
     39                // regExp: String 
     40                //              regular expression string used to validate the input 
     41                //              Do not specify both regExp and regExpGen 
     42                regExp: ".*", 
     43                // regExpGen: Function 
     44                //              user replaceable function used to generate regExp when dependent on flags 
     45                //              Do not specify both regExp and regExpGen 
     46                regExpGen: function(flags){ return this.regExp; }, 
    5347                lastCheckedValue: null, 
    5448         
    5549                templatePath: dojo.uri.moduleUri("dijit.form", "templates/ValidationTextbox.html"), 
    5650                 
    57                 // new DOM nodes 
    58                 invalidSpan: null, 
    59                 missingSpan: null, 
    60                 rangeSpan: null, 
    61  
    6251                getValue: function() { 
    6352                        return this.textbox.value; 
     
    6958                }, 
    7059         
     60                validator: function(value,flags){ 
     61                        // summary: user replaceable function used to validate the text input against the regular expression. 
     62                        return (new RegExp("^(" + this.regExpGen(flags) + ")$")).test(value); 
     63                }, 
     64 
    7165                isValid: function() { 
    7266                        // summary: Need to over-ride with your own validation code in subclasses 
    73                         return true; 
     67                        return this.validator(this.textbox.value, this.flags); 
    7468                }, 
    7569         
     70                rangeCheck: function(value,flags){ 
     71                        // summary: user replaceable function used to validate the range of the numeric input value 
     72                        if ((typeof flags.min == "number") || (typeof flags.min == "number")){ 
     73                                return dojo.validate.isInRange(value, flags); 
     74                        }else{ return true; } 
     75                }, 
     76 
    7677                isInRange: function() { 
    7778                        // summary: Need to over-ride with your own validation code in subclasses 
    78                         return true; 
     79                        return this.rangeCheck(this.textbox.value, this.flags); 
    7980                }, 
    8081         
     
    9596                        //              Show missing or invalid messages if appropriate, and highlight textbox field. 
    9697                        this.lastCheckedValue = this.textbox.value; 
    97                         this.missingSpan.style.display = "none"; 
    98                         this.invalidSpan.style.display = "none"; 
    99                         this.rangeSpan.style.display = "none"; 
    10098         
    10199                        var empty = this.isEmpty(); 
     
    108106                        // Display at most one error message 
    109107                        if(missing){ 
    110                                 this.missingSpan.style.display = ""; 
     108                                this.messageSpan.innerHTML = this.missingMessage; 
    111109                        }else if( !empty && !valid ){ 
    112                                 this.invalidSpan.style.display = ""; 
     110                                this.messageSpan.innerHTML = this.invalidMessage; 
    113111                        }else if( !empty && !this.isInRange() ){ 
    114                                 this.rangeSpan.style.display = ""; 
     112                                this.messageSpan.innerHTML = this.rangeMessage; 
     113                        }else{ 
     114                                this.messageSpan.innerHTML = ""; 
    115115                        } 
     116 
    116117                        this.highlight(); 
    117118                }, 
     
    119120                updateClass: function(className){ 
    120121                        // summary: used to ensure that only 1 validation class is set at a time 
    121                         var pre = this.classPrefix; 
    122                         dojo.html.removeClass(this.textbox,pre+"Empty"); 
    123                         dojo.html.removeClass(this.textbox,pre+"Valid"); 
    124                         dojo.html.removeClass(this.textbox,pre+"Invalid"); 
    125                         dojo.html.addClass(this.textbox,pre+className); 
     122                        dojo.html.removeClass(this.textbox,"dojoInputFieldValidationWarning"); 
     123                        dojo.html.removeClass(this.textbox,"dojoInputFieldValidationError"); 
     124                        dojo.html.addClass(this.textbox,className); 
    126125                }, 
    127126                 
     
    131130                        // highlight textbox background  
    132131                        if (this.isEmpty()) { 
    133                                 this.updateClass("Empty"); 
     132                                this.updateClass("dojoInputFieldValidationError"); 
    134133                        }else if (this.isValid() && this.isInRange() ){ 
    135                                 this.updateClass("Valid"); 
     134                                this.updateClass("dojoInputField"); 
    136135                        }else if(this.textbox.value != this.promptMessage){  
    137                                 this.updateClass("Invalid"); 
     136                                this.updateClass("dojoInputFieldValidationError"); 
    138137                        }else{ 
    139                                 this.updateClass("Empty"); 
     138                                this.updateClass("dojoInputFieldValidationWarning"); 
    140139                        } 
    141140                }, 
     
    143142                onfocus: function(evt) { 
    144143                        if ( !this.listenOnKeyPress) { 
    145                                 this.updateClass("Empty"); 
     144                                this.updateClass("dojoInputFieldValidationWarning"); 
    146145                        } 
    147146                }, 
     
    157156                                this.update();  
    158157                        }else if (this.textbox.value != this.lastCheckedValue){ 
    159                                 this.updateClass("Empty"); 
     158                                this.updateClass("dojoInputFieldValidationWarning"); 
    160159                        } 
    161160                }, 
     
    165164                        this.messages = dojo.i18n.getLocalization("dijit.form", "validate", this.lang); 
    166165                        dojo.lang.forEach(["invalidMessage", "missingMessage", "rangeMessage"], function(prop) { 
    167                                 if(this[prop]){ this.messages[prop] = this[prop]; } 
     166                                if(!this[prop]){ this[prop] = this.messages[prop]; } 
    168167                        }, this); 
     168                        var p = this.regExpGen(this.flags); 
     169                        this.regExp = p; 
    169170                }, 
    170171         
     
    179180                        this.textbox.isInRange = function() { this.isInRange.call(this); }; 
    180181                        this.update();  
    181                          
    182                         // apply any filters to initial value 
    183                         this.filter(); 
    184182                } 
    185183        } 
  • dijit/trunk/tests/form/test_validate.html

    r7276 r7283  
    5353                } 
    5454         
    55                 span.invalid, span.missing { 
     55                .dojoValidationTextboxMessage { 
    5656                        display: inline; 
    5757                        margin-left: 1em; 
     
    6363                } 
    6464         
    65                 .errorMessage { 
    66                         font-weight:bold; 
    67                         font-family:Arial, Verdana, sans-serif; 
    68                         color:#ff0000; 
    69                         font-size:0.9em; 
    70                 } 
    71                 .warningMessage { 
    72                         font-weight:bold; 
    73                         font-family:Arial, Verdana, sans-serif; 
    74                         color:#ff9900; 
    75                         font-size:0.9em; 
    76                 } 
    7765                .noticeMessage { 
    7866                        font-weight: normal; 
     
    8169                        font-size:0.9em; 
    8270                } 
    83                 .myValidateColorValid { 
    84                         background-color: #FF0000; 
    85                 } 
    86                 .myValidateColorInvalid { 
    87                         background-color: #00FF00; 
    88                 } 
    89                 .myValidateColorEmpty { 
    90                         background-color: #0000FF; 
    91                 } 
    9271                </style> 
    9372        </head> 
     
    121100                        <div class="formQuestion"> 
    122101                                <span class="emphasise">Age:  </span> 
    123                                 <span class="noticeMessage"> Textbox class, Attributes: {trim: true, digit: true, class: 'small'}, all but digits extracted. </span> 
     102                                <span class="noticeMessage"> Textbox class, Attributes: {trim: true, digit: true, class: 'small'}, all but digits extracted.</span> 
    124103                        </div> 
    125104                        <div class="formAnswer"> 
     
    143122                        </div> 
    144123 
    145 <!-- 
    146 TODO: uncomment when other validation widgets have been ported over. 
    147124                        <div class="formQuestion"> 
    148125                                <span class="emphasise">Elevation:  </span> 
    149126                                <span class="noticeMessage">IntegerTextbox class,  
    150                                         Attributes: {trim: true, required: true, signed: true classPrefix: "myValidateColor"}, Enter feet above sea level with a sign. (note: these colors are drastically different due to demonstrating the effects of classPrefix.)</span> 
     127                                        Attributes: {trim: true, required: true, signed: true }, Enter feet above sea level with a sign.</span> 
    151128                        </div> 
    152129                        <div class="formAnswer"> 
    153130                                <input type="text" name="elevation" class="medium" value="300" 
    154                                         dojoType="dijit.form.IntegerTextbox" 
     131                                        dojoType="dijit.form.ValidationTextbox" 
     132                                        regExpGen="dojo.regexp.integer" 
     133                                        flags="{signed:true}" 
    155134                                        trim="true" 
    156                                         classPrefix="myValidateColor" 
    157                                         required="true"  
    158                                         signed="true"  
     135                                        required="true"  
    159136                                        invalidMessage="Invalid elevation.  Be sure to use a plus or minus sign." /> 
    160137                        </div> 
     
    177154                        trim: "true", 
    178155                        required: "true", 
    179                         signed: "false", 
    180                         separator: ",", 
     156                        regExpGen: dojo.regexp.integer, 
     157                        flags: {signed:false, separator: ","}, 
    181158                        invalidMessage: "Invalid population.  Be sure to use commas." 
    182159                }; 
    183160                var refNode = document.getElementById("attach-here"); 
    184161 
    185                 var w = new dijit.form.IntegerTextbox(name, props, refNode); 
     162                var w = new dijit.form.ValidationTextbox(props, refNode); 
    186163        }); 
    187164</script> 
     165<!-- 
     166TODO: uncomment when other validation widgets have been ported over. 
    188167 
    189168                        <div class="formQuestion"> 
  • dijit/trunk/themes/tundra/tundra.css

    r7276 r7283  
    4545 
    4646/* Default Disabled Styles*/ 
     47.dojoButton[disabled], 
     48.dojoInputField[disabled], 
    4749.dojoButtonDisabled, 
    4850.dojoInputFieldDisabled { 
     
    5355} 
    5456 
    55 .dojoButtonDisabled:hover, 
    56 .dojoInputFieldDisabled:hover { 
    57         color:#787878; 
    58  
    59 } 
    60  
     57.dojoButton[disabled]:active, 
     58.dojoInputField[disabled]:active, 
    6159.dojoButtonDisabled:active, 
    6260.dojoInputFieldDisabled:active { 
     
    7371} 
    7472 
    75 .dojoInputFieldDisabled:hover { 
    76         border-color:#e0e0e0; 
     73.dojoInputFieldValidationWarning, 
     74.dojoInputFieldValidationWarning:hover, 
     75.dojoInputFieldValidationWarning:focus { 
     76/* TBD */ 
     77        background-color:cyan; 
    7778} 
    7879 
     
    9091        background-image: url(check.gif); 
    9192} 
    92  
    93 .dojoValidateEmpty{ 
    94         background-color: #00FFFF; 
    95 } 
    96 .dojoValidateValid{ 
    97         background-color: #cfc; 
    98 } 
    99 .dojoValidateInvalid{ 
    100         background-color: #fcc; 
    101 } 
    102 .dojoValidateRange{ 
    103         background-color: #ccf; 
    104 } 
  • dijit/trunk/util/parser.js

    r7229 r7283  
    3030                switch(type){ 
    3131                        case "string": 
    32                         default: 
    3332                                return value; 
    3433                        case "number": 
     
    3837                        case "function": 
    3938                                if(value.search(/[^\w\.]+/i) == -1){ 
    40                                         return dojo.evalObjPath(value, false); 
     39                                        return dojo.getObject(value, false); 
    4140                                }else{ 
    4241                                        try{ 
     
    5150                        case "uri": 
    5251                                return dojo.uri.dojoUri(value); 
     52                        default: 
     53                                try { eval("var tmp = "+value); return tmp; } 
     54                                catch(e) { return value; } 
    5355                } 
    5456        }; 
     
    103105                                for(var attrName in clsInfo.params){ 
    104106                                        var attrValue = node.getAttribute(attrName); 
    105                                         if(attrValue){ 
     107                                        if(attrValue != null){ 
    106108                                                var attrType = clsInfo.params[attrName]; 
    107109                                                params[attrName] = str2obj(attrValue, attrType);