r49198 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r49197‎ | r49198 | r49199 >
Date:09:49, 5 April 2009
Author:werdna
Status:deferred
Tags:
Comment:
Add validation for form fields in HTMLForm
Modified paths:
  • /branches/preferences-work/phase3/includes/HTMLForm.php (modified) (history)
  • /branches/preferences-work/phase3/includes/specials/SpecialRecentchangeslinked.php (modified) (history)

Diff [purge]

Index: branches/preferences-work/phase3/includes/HTMLForm.php
@@ -84,6 +84,14 @@
8585 return false;
8686 }
8787
 88+ // Check for validation
 89+ foreach( $this->mFlatFields as $fieldname => $field ) {
 90+ if ( !$field->validate( $this->mFieldData[$fieldname] ) ) {
 91+ return isset($this->mValidationErrorMessage) ?
 92+ $this->mValidationErrorMessage : array( 'htmlform-invalid-input' );
 93+ }
 94+ }
 95+
8896 $callback = $this->mSubmitCallback;
8997
9098 $res = call_user_func( $callback, $this->mFieldData );
@@ -95,6 +103,10 @@
96104 $this->mSubmitCallback = $cb;
97105 }
98106
 107+ function setValidationErrorMessage( $msg ) {
 108+ $this->mValidationErrorMessage = $msg;
 109+ }
 110+
99111 function displayForm( $submitResult ) {
100112 global $wgUser, $wgOut;
101113
@@ -133,7 +145,7 @@
134146 $wgOut->addHTML( $errorstr );
135147 }
136148
137 - function formatErrors( $errors ) {
 149+ static function formatErrors( $errors ) {
138150 $errorstr = '';
139151 foreach ( $errors as $error ) {
140152 if (is_array($error)) {
@@ -209,6 +221,14 @@
210222 abstract class HTMLFormField {
211223 abstract function getInputHTML( $value );
212224
 225+ function validate( $value ) {
 226+ if ( isset($this->mValidationCallback) ) {
 227+ return call_user_func( $this->mValidationCallback, $value );
 228+ }
 229+
 230+ return true;
 231+ }
 232+
213233 function loadDataFromRequest( $request ) {
214234 if ($request->getCheck( $this->mName ) ) {
215235 return $request->getText( $this->mName );
@@ -247,16 +267,26 @@
248268 if ( isset( $params['id'] ) ) {
249269 $this->mID = $params['id'];
250270 }
 271+
 272+ if ( isset( $params['validation-callback'] ) ) {
 273+ $this->mValidationCallback = $params['validation-callback'];
 274+ }
251275 }
252276
253277 function getTableRow( $value ) {
 278+ // Check for invalid data.
 279+ $errors = $this->validate( $value );
 280+ if ( $errors === true ) {
 281+ $errors = '';
 282+ }
 283+
254284 $html = '';
255285
256286 $html .= Xml::tags( 'td', null,
257287 Xml::tags( 'label', array( 'for' => $this->mID ), $this->getLabel() )
258288 );
259289 $html .= Xml::tags( 'td', array( 'class' => 'mw-input' ),
260 - $this->getInputHTML( $value ) );
 290+ $this->getInputHTML( $value ) ."\n$errors" );
261291
262292 $html = Xml::tags( 'tr', null, $html ) . "\n";
263293
Index: branches/preferences-work/phase3/includes/specials/SpecialRecentchangeslinked.php
@@ -90,7 +90,7 @@
9191 $conds,
9292 $join_conds,
9393 $query_options,
94 - $opts['tagfilter'],
 94+ $opts['tagfilter']
9595 );
9696
9797 // XXX: parent class does this, should we too?

Status & tagging log