Index: branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js |
— | — | @@ -54,7 +54,18 @@ |
55 | 55 | return testOptional( value, element ) || /^-?\d+$/.test(value); |
56 | 56 | }, mw.msg( 'gadgets-formbuilder-integer' ) ); |
57 | 57 | |
| 58 | + //validator for datepicker fields |
| 59 | + $.validator.addMethod( "datePicker", function( value, element ) { |
| 60 | + var format = $( element ).datepicker( 'option', 'dateFormat' ); |
| 61 | + try { |
| 62 | + var date = $.datepicker.parseDate( format, value ); |
| 63 | + return true; |
| 64 | + } catch ( e ) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + }, mw.msg( 'gadgets-formbuilder-date' ) ); |
58 | 68 | |
| 69 | + |
59 | 70 | //Helper function for inheritance, see http://javascript.crockford.com/prototypal.html |
60 | 71 | function object(o) { |
61 | 72 | function F() {} |
— | — | @@ -361,20 +372,30 @@ |
362 | 373 | .attr( 'type', 'text' ) |
363 | 374 | .attr( 'id', idPrefix + this.name ) |
364 | 375 | .attr( 'name', idPrefix + this.name ) |
365 | | - .datepicker(); |
| 376 | + .datepicker( { |
| 377 | + onSelect: function() { |
| 378 | + //Force validation, so that a previous 'invalid' state is removed |
| 379 | + $( this ).valid(); |
| 380 | + } |
| 381 | + } ); |
366 | 382 | |
367 | 383 | if ( desc.value !== null ) { |
368 | 384 | this.$text.datepicker( 'setDate', date ); |
369 | 385 | } |
370 | | - |
| 386 | + |
| 387 | + |
371 | 388 | this.$p.append( this.$text ); |
372 | 389 | } |
373 | 390 | |
374 | 391 | DateField.prototype.getValue = function() { |
375 | | - function pad( n ) { |
376 | | - return n < 10 ? '0' + n : n; |
| 392 | + function pad( n, len ) { |
| 393 | + var res = '' + n; |
| 394 | + while ( res.length < len ) { |
| 395 | + res = '0' + res; |
| 396 | + } |
| 397 | + return res; |
377 | 398 | } |
378 | | - |
| 399 | + |
379 | 400 | var d = this.$text.datepicker( 'getDate' ); |
380 | 401 | |
381 | 402 | if ( d === null ) { |
— | — | @@ -382,16 +403,24 @@ |
383 | 404 | } |
384 | 405 | |
385 | 406 | //UTC date in ISO 8601 format [YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]Z |
386 | | - return '' + d.getUTCFullYear() + '-' + |
387 | | - pad( d.getUTCMonth() + 1 ) + '-' + |
388 | | - pad( d.getUTCDate() ) + 'T' + |
389 | | - pad( d.getUTCHours() ) + ':' + |
390 | | - pad( d.getUTCMinutes() ) + ':' + |
391 | | - pad( d.getUTCSeconds() ) + 'Z'; |
| 407 | + return '' + pad( d.getUTCFullYear(), 4 ) + '-' + |
| 408 | + pad( d.getUTCMonth() + 1, 2 ) + '-' + |
| 409 | + pad( d.getUTCDate(), 2 ) + 'T' + |
| 410 | + pad( d.getUTCHours(), 2 ) + ':' + |
| 411 | + pad( d.getUTCMinutes(), 2 ) + ':' + |
| 412 | + pad( d.getUTCSeconds(), 2 ) + 'Z'; |
392 | 413 | }; |
393 | 414 | |
| 415 | + DateField.prototype.getValidationSettings = function() { |
| 416 | + var settings = LabelField.prototype.getValidationSettings.call( this ), |
| 417 | + fieldId = idPrefix + this.name; |
| 418 | + |
| 419 | + settings.rules[fieldId] = { |
| 420 | + "datePicker": true |
| 421 | + }; |
| 422 | + return settings; |
| 423 | + } |
394 | 424 | |
395 | | - |
396 | 425 | var validFieldTypes = { |
397 | 426 | "boolean": BooleanField, |
398 | 427 | "string" : StringField, |
Index: branches/salvatoreingala/Gadgets/Gadgets.i18n.php |
— | — | @@ -59,6 +59,7 @@ |
60 | 60 | 'gadgets-formbuilder-min' => 'Please enter a value not less than $1.', |
61 | 61 | 'gadgets-formbuilder-max' => 'Please enter a value not greater than $1.', |
62 | 62 | 'gadgets-formbuilder-integer' => 'Please enter an integer number.', |
| 63 | + 'gadgets-formbuilder-date' => 'Please enter a valid date.', |
63 | 64 | ); |
64 | 65 | |
65 | 66 | /** Message documentation (Message documentation) |
Index: branches/salvatoreingala/Gadgets/Gadgets.php |
— | — | @@ -77,7 +77,7 @@ |
78 | 78 | 'dependencies' => array( 'jquery', 'jquery.ui.slider', 'jquery.ui.datepicker', 'jquery.validate' ), |
79 | 79 | 'messages' => array( |
80 | 80 | 'gadgets-formbuilder-required', 'gadgets-formbuilder-minlength', 'gadgets-formbuilder-maxlength', |
81 | | - 'gadgets-formbuilder-min', 'gadgets-formbuilder-max', 'gadgets-formbuilder-integer' |
| 81 | + 'gadgets-formbuilder-min', 'gadgets-formbuilder-max', 'gadgets-formbuilder-integer', 'gadgets-formbuilder-date' |
82 | 82 | ), |
83 | 83 | 'localBasePath' => $dir . 'ui/resources/', |
84 | 84 | 'remoteExtPath' => 'Gadgets/ui/resources' |