r83809 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83808‎ | r83809 | r83810 >
Date:09:51, 13 March 2011
Author:nikerabbit
Status:ok
Tags:
Comment:
HTMLForm:
* Allow field validators to return multiple errors
* Allow field validators to return Message objects
* Add a class for fields for invalid input
* Style invalid <input> fields with red border color

Done with http://www.mediawiki.org/wiki/StyleGuide/Forms in mind
Modified paths:
  • /trunk/phase3/includes/HTMLForm.php (modified) (history)
  • /trunk/phase3/skins/common/shared.css (modified) (history)

Diff [purge]

Index: trunk/phase3/skins/common/shared.css
@@ -123,6 +123,10 @@
124124 text-align: left !important;
125125 }
126126
 127+.mw-htmlform-invalid-input td.mw-input input {
 128+ border-color: red;
 129+}
 130+
127131 input#wpSummary {
128132 width: 80%;
129133 }
@@ -617,7 +621,7 @@
618622 .tipsy-arrow {
619623 position: absolute;
620624 /* @embed */
621 - background: url( 'images/tipsy-arrow.gif' ) no-repeat top left;
 625+ background: url(images/tipsy-arrow.gif) no-repeat top left;
622626 width: 13px;
623627 height: 13px;
624628 }
Index: trunk/phase3/includes/HTMLForm.php
@@ -890,8 +890,10 @@
891891
892892 if ( $errors === true || ( !$wgRequest->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) {
893893 $errors = '';
 894+ $errorClass = '';
894895 } else {
895 - $errors = Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
 896+ $errors = self::formatErrors( $errors );
 897+ $errorClass = 'mw-htmlform-invalid-input';
896898 }
897899
898900 $label = $this->getLabelHtml( $cellAttributes );
@@ -903,15 +905,15 @@
904906
905907 $fieldType = get_class( $this );
906908
907 - if ($verticalLabel) {
 909+ if ( $verticalLabel ) {
908910 $html = Html::rawElement( 'tr',
909911 array( 'class' => 'mw-htmlform-vertical-label' ), $label );
910912 $html .= Html::rawElement( 'tr',
911 - array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass}" ),
 913+ array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
912914 $field );
913915 } else {
914916 $html = Html::rawElement( 'tr',
915 - array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass}" ),
 917+ array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
916918 $label . $field );
917919 }
918920
@@ -1009,6 +1011,33 @@
10101012
10111013 return $flatOpts;
10121014 }
 1015+
 1016+ /**
 1017+ * @param $errors String|Message|Array of strings or Message instances
 1018+ * @return String html
 1019+ */
 1020+ protected static function formatErrors( $errors ) {
 1021+ if ( is_array( $errors ) && count( $errors ) === 1 ) {
 1022+ $errors = array_shift( $errors );
 1023+ }
 1024+
 1025+ if ( is_array( $errors ) ) {
 1026+ $lines = array();
 1027+ foreach ( $errors as $error ) {
 1028+ if ( $error instanceof Message ) {
 1029+ $lines[] = Html::rawElement( 'li', array(), $error->parse() );
 1030+ } else {
 1031+ $lines[] = Html::rawElement( 'li', array(), $error );
 1032+ }
 1033+ }
 1034+ return Html::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
 1035+ } else {
 1036+ if ( $errors instanceof Message ) {
 1037+ $errors = $errors->parse();
 1038+ }
 1039+ return Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
 1040+ }
 1041+ }
10131042 }
10141043
10151044 class HTMLTextField extends HTMLFormField {

Follow-up revisions

RevisionCommit summaryAuthorDate
r83811Follow-up r83809 r83810: improved documentationnikerabbit10:02, 13 March 2011

Status & tagging log