r71479 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71478‎ | r71479 | r71480 >
Date:13:21, 23 August 2010
Author:yaron
Status:deferred
Tags:
Comment:
Replaced initialization of YUI with that of jQuery; added fixes for validation of mandatory fields, in some cases due to change of input layout due to 'show on select' feature; made validation of some types more tolerant of whitespaces; also simplified some validation using jQuery
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormUtils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormUtils.php
@@ -28,10 +28,19 @@
2929 $javascript_text = <<<END
3030
3131 function validate_mandatory_field(field_id, info_id) {
32 - field = document.getElementById(field_id);
 32+ var field = document.getElementById(field_id);
 33+ // if there's nothing at that field ID, ignore it - it's probably
 34+ // a hidden field
 35+ if (field == null) {
 36+ return true;
 37+ }
3338 if (field.value.replace(/\s+/, '') == '') {
34 - infobox = document.getElementById(info_id);
35 - infobox.innerHTML = "$blank_error_str";
 39+ var info_span = document.getElementById(info_id);
 40+ if ( info_span == null ) {
 41+ alert ("no info span found at " + info_id + "!");
 42+ } else {
 43+ info_span.innerHTML = "$blank_error_str";
 44+ }
3645 return false;
3746 } else {
3847 return true;
@@ -43,27 +52,51 @@
4453 function validate_mandatory_radiobutton(none_button_id, info_id) {
4554 none_button = document.getElementById(none_button_id);
4655 if (none_button && none_button.checked) {
47 - infobox = document.getElementById(info_id);
48 - infobox.innerHTML = "$blank_error_str";
 56+ info_span = document.getElementById(info_id);
 57+ info_span.innerHTML = "$blank_error_str";
4958 return false;
5059 } else {
5160 return true;
5261 }
5362 }
5463
 64+function validate_mandatory_combobox(field_id, info_id) {
 65+ var field = jQuery('input#' + field_id);
 66+ // if there's nothing at that field ID, ignore it - it's probably
 67+ // a hidden field
 68+ if (field == null) {
 69+ return true;
 70+ }
 71+ // FIXME
 72+ // field.val() unfortunately doesn't work in IE - it just returns
 73+ // "undefined". For now, if that happens, just exit
 74+ var value = field.val();
 75+ if (value == undefined) {
 76+ alert(field.html());
 77+ return true;
 78+ }
 79+ if (value.replace(/\s+/, '') == '') {
 80+ var info_span = document.getElementById(info_id);
 81+ info_span.innerHTML = "$blank_error_str";
 82+ return false;
 83+ } else {
 84+ return true;
 85+ }
 86+}
 87+
5588 function validate_mandatory_checkboxes(field_id, info_id) {
56 - elems = document.getElementsByTagName("*");
57 - var all_fields_unchecked = true;
58 - for (var i = 0; i < elems.length; i++) {
59 - if (elems[i].id == field_id) {
60 - if (elems[i].checked) {
61 - all_fields_unchecked = false;
62 - }
 89+ // get all checkboxes - the "field_id" in this case is the span
 90+ // surrounding all the checkboxes
 91+ var checkboxes = jQuery('span#' + field_id + " > span > input");
 92+ var all_unchecked = true;
 93+ for (var i = 0; i < checkboxes.length; i++) {
 94+ if (checkboxes[i].checked) {
 95+ all_unchecked = false;
6396 }
6497 }
65 - if (all_fields_unchecked) {
66 - infobox = document.getElementById(info_id);
67 - infobox.innerHTML = "$blank_error_str";
 98+ if (all_unchecked) {
 99+ info_span = document.getElementById(info_id);
 100+ info_span.innerHTML = "$blank_error_str";
68101 return false;
69102 } else {
70103 return true;
@@ -102,26 +135,26 @@
103136 if (url_regexp.test(field.value)) {
104137 return true;
105138 } else {
106 - infobox = document.getElementById(info_id);
107 - infobox.innerHTML = "$bad_url_error_str";
 139+ info_span = document.getElementById(info_id);
 140+ info_span.innerHTML = "$bad_url_error_str";
108141 return false;
109142 }
110143 } else if (type == 'email') {
111144 // code borrowed from http://javascript.internet.com/forms/email-validation---basic.html
112 - var email_regexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/;
 145+ var email_regexp = /^\s*\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+\s*$/;
113146 if (email_regexp.test(field.value)) {
114147 return true;
115148 } else {
116 - infobox = document.getElementById(info_id);
117 - infobox.innerHTML = "$bad_email_error_str";
 149+ info_span = document.getElementById(info_id);
 150+ info_span.innerHTML = "$bad_email_error_str";
118151 return false;
119152 }
120153 } else if (type == 'number') {
121 - if (field.value.match(/^\-?[\d\.,]+$/)) {
 154+ if (field.value.match(/^\s*\-?[\d\.,]+\s*$/)) {
122155 return true;
123156 } else {
124 - infobox = document.getElementById(info_id);
125 - infobox.innerHTML = "$bad_number_error_str";
 157+ info_span = document.getElementById(info_id);
 158+ info_span.innerHTML = "$bad_number_error_str";
126159 return false;
127160 }
128161 } else if (type == 'date') {
@@ -136,8 +169,8 @@
137170 // 'BC' and possibly other non-number strings
138171 return true;
139172 } else {
140 - infobox = document.getElementById(info_id);
141 - infobox.innerHTML = "$bad_date_error_str";
 173+ info_span = document.getElementById(info_id);
 174+ info_span.innerHTML = "$bad_date_error_str";
142175 return false;
143176 }
144177 } else {
@@ -278,7 +311,11 @@
279312 }
280313
281314 static function autocompletionJavascript() {
282 - global $wgScriptPath;
 315+ global $wgScriptPath, $wgOut, $smwgScriptPath, $smwgJQueryIncluded;
 316+ if ( !$smwgJQueryIncluded ) {
 317+ $wgOut->addScriptFile( "$smwgScriptPath/libs/jquery-1.4.2.min.js" );
 318+ $smwgJQueryIncluded = true;
 319+ }
283320
284321 $javascript_text = <<<END
285322 var autocompletemappings = new Array();
@@ -343,7 +380,7 @@
344381 }
345382 }
346383
347 -YAHOO.util.Event.addListener(window, 'load', attachAutocompleteToAllDocumentFields);
 384+jQuery.event.add(window, "load", attachAutocompleteToAllDocumentFields);
348385
349386 END;
350387 return $javascript_text;
@@ -399,9 +436,9 @@
400437 return Xml::expandAttributes( $attribs );
401438 } else {
402439 $out = '';
403 - foreach ( $attribs as $name => $val )
404 - $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
405 - return $out;
 440+ foreach ( $attribs as $name => $val )
 441+ $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
 442+ return $out;
406443 }
407444 }
408445
@@ -711,13 +748,13 @@
712749 ';
713750
714751 $showRef = 'false';
715 - if ( (isset($wgHooks['ParserFirstCallInit']) && in_array('wfCite',$wgHooks['ParserFirstCallInit'])) || (isset($wgExtensionFunctions) && in_array('wfCite',$wgExtensionFunctions)) ) {
 752+ if ( ( isset( $wgHooks['ParserFirstCallInit'] ) && in_array( 'wfCite', $wgHooks['ParserFirstCallInit'] ) ) || ( isset( $wgExtensionFunctions ) && in_array( 'wfCite', $wgExtensionFunctions ) ) ) {
716753 $showRef = 'true';
717754 }
718755
719756 $showSource = 'false';
720 - if ( (isset ($wgHooks['ParserFirstCallInit']) && in_array('efSyntaxHighlight_GeSHiSetup', $wgHooks['ParserFirstCallInit']))
721 - || (isset ($wgExtensionFunctions) && in_array('efSyntaxHighlight_GeSHiSetup', $wgExtensionFunctions)) ) {
 757+ if ( ( isset( $wgHooks['ParserFirstCallInit'] ) && in_array( 'efSyntaxHighlight_GeSHiSetup', $wgHooks['ParserFirstCallInit'] ) )
 758+ || ( isset( $wgExtensionFunctions ) && in_array( 'efSyntaxHighlight_GeSHiSetup', $wgExtensionFunctions ) ) ) {
722759 $showSource = 'true';
723760 }
724761

Status & tagging log