r91486 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91485‎ | r91486 | r91487 >
Date:20:28, 5 July 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
Added simple client-side validation for 'date' fields.
Modified paths:
  • /branches/salvatoreingala/Gadgets/Gadgets.i18n.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/Gadgets.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js
@@ -54,7 +54,18 @@
5555 return testOptional( value, element ) || /^-?\d+$/.test(value);
5656 }, mw.msg( 'gadgets-formbuilder-integer' ) );
5757
 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' ) );
5868
 69+
5970 //Helper function for inheritance, see http://javascript.crockford.com/prototypal.html
6071 function object(o) {
6172 function F() {}
@@ -361,20 +372,30 @@
362373 .attr( 'type', 'text' )
363374 .attr( 'id', idPrefix + this.name )
364375 .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+ } );
366382
367383 if ( desc.value !== null ) {
368384 this.$text.datepicker( 'setDate', date );
369385 }
370 -
 386+
 387+
371388 this.$p.append( this.$text );
372389 }
373390
374391 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;
377398 }
378 -
 399+
379400 var d = this.$text.datepicker( 'getDate' );
380401
381402 if ( d === null ) {
@@ -382,16 +403,24 @@
383404 }
384405
385406 //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';
392413 };
393414
 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+ }
394424
395 -
396425 var validFieldTypes = {
397426 "boolean": BooleanField,
398427 "string" : StringField,
Index: branches/salvatoreingala/Gadgets/Gadgets.i18n.php
@@ -59,6 +59,7 @@
6060 'gadgets-formbuilder-min' => 'Please enter a value not less than $1.',
6161 'gadgets-formbuilder-max' => 'Please enter a value not greater than $1.',
6262 'gadgets-formbuilder-integer' => 'Please enter an integer number.',
 63+ 'gadgets-formbuilder-date' => 'Please enter a valid date.',
6364 );
6465
6566 /** Message documentation (Message documentation)
Index: branches/salvatoreingala/Gadgets/Gadgets.php
@@ -77,7 +77,7 @@
7878 'dependencies' => array( 'jquery', 'jquery.ui.slider', 'jquery.ui.datepicker', 'jquery.validate' ),
7979 'messages' => array(
8080 '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'
8282 ),
8383 'localBasePath' => $dir . 'ui/resources/',
8484 'remoteExtPath' => 'Gadgets/ui/resources'

Status & tagging log