/**
* Add the HTMLForm-specific JavaScript, if it hasn't been
* done already.
- * @deprecated @since 1.18 load modules with ResourceLoader instead
+ * @deprecated since 1.18 load modules with ResourceLoader instead
*/
static function addJS() { }
* @param $errors Array of message keys/values
* @return String HTML, a <ul> list of errors
*/
- static function formatErrors( $errors ) {
+ public static function formatErrors( $errors ) {
$errorstr = '';
foreach ( $errors as $error ) {
$hasLeftColumn = true;
} elseif ( is_array( $value ) ) {
$section = $this->displaySection( $value, $key );
- $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
+ $legend = $this->getLegend( $key );
if ( isset( $this->mSectionHeaders[$key] ) ) {
$section = $this->mSectionHeaders[$key] . $section;
}
function filterDataForSubmit( $data ) {
return $data;
}
+
+ /**
+ * Get a string to go in the <legend> of a section fieldset. Override this if you
+ * want something more complicated
+ * @param $key String
+ * @return String
+ */
+ public function getLegend( $key ) {
+ return wfMsg( "{$this->mMessagePrefix}-$key" );
+ }
}
/**
if ( $errors === true || ( !$wgRequest->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) {
$errors = '';
+ $errorClass = '';
} else {
- $errors = Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
+ $errors = self::formatErrors( $errors );
+ $errorClass = 'mw-htmlform-invalid-input';
}
$label = $this->getLabelHtml( $cellAttributes );
$fieldType = get_class( $this );
- if ($verticalLabel) {
+ if ( $verticalLabel ) {
$html = Html::rawElement( 'tr',
array( 'class' => 'mw-htmlform-vertical-label' ), $label );
$html .= Html::rawElement( 'tr',
- array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass}" ),
+ array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
$field );
} else {
$html = Html::rawElement( 'tr',
- array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass}" ),
+ array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
$label . $field );
}
if ( isset( $this->mParams['help-message'] ) ) {
$msg = $this->mParams['help-message'];
$helptext = wfMsgExt( $msg, 'parseinline' );
- if ( wfEmptyMsg( $msg, $helptext ) ) {
+ if ( wfEmptyMsg( $msg ) ) {
# Never mind
$helptext = null;
}
return $flatOpts;
}
+
+ /**
+ * Formats one or more errors as accepted by field validation-callback.
+ * @param $errors String|Message|Array of strings or Message instances
+ * @return String html
+ * @since 1.18
+ */
+ protected static function formatErrors( $errors ) {
+ if ( is_array( $errors ) && count( $errors ) === 1 ) {
+ $errors = array_shift( $errors );
+ }
+
+ if ( is_array( $errors ) ) {
+ $lines = array();
+ foreach ( $errors as $error ) {
+ if ( $error instanceof Message ) {
+ $lines[] = Html::rawElement( 'li', array(), $error->parse() );
+ } else {
+ $lines[] = Html::rawElement( 'li', array(), $error );
+ }
+ }
+ return Html::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
+ } else {
+ if ( $errors instanceof Message ) {
+ $errors = $errors->parse();
+ }
+ return Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
+ }
+ }
}
class HTMLTextField extends HTMLFormField {
}
// GetCheck won't work like we want for checks.
- if ( $request->getCheck( 'wpEditToken' ) ) {
+ if ( $request->getCheck( 'wpEditToken' ) || $this->mParent->getMethod() != 'post' ) {
// XOR has the following truth table, which is what we want
// INVERT VALUE | OUTPUT
// true true | false
* Multi-select field
*/
class HTMLMultiSelectField extends HTMLFormField {
+
+ public function __construct( $params ){
+ parent::__construct( $params );
+ if( isset( $params['flatlist'] ) ){
+ $this->mClass .= ' mw-htmlform-multiselect-flatlist';
+ }
+ }
+
function validate( $value, $alldata ) {
$p = parent::validate( $value, $alldata );
$attribs + $thisAttribs );
$checkbox .= ' ' . Html::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
- $html .= $checkbox . '<br />';
+ $html .= ' ' . Html::rawElement( 'div', array( 'class' => 'mw-htmlform-multiselect-item' ), $checkbox );
}
}
}
function loadDataFromRequest( $request ) {
- # won't work with getCheck
- if ( $request->getCheck( 'wpEditToken' ) ) {
- $arr = $request->getArray( $this->mName );
-
- if ( !$arr ) {
- $arr = array();
+ if ( $this->mParent->getMethod() == 'post' ) {
+ if( $request->wasPosted() ){
+ # Checkboxes are just not added to the request arrays if they're not checked,
+ # so it's perfectly possible for there not to be an entry at all
+ return $request->getArray( $this->mName, array() );
+ } else {
+ # That's ok, the user has not yet submitted the form, so show the defaults
+ return $this->getDefault();
}
-
- return $arr;
} else {
- return $this->getDefault();
+ # This is the impossible case: if we look at $_GET and see no data for our
+ # field, is it because the user has not yet submitted the form, or that they
+ # have submitted it with all the options unchecked? We will have to assume the
+ # latter, which basically means that you can't specify 'positive' defaults
+ # for GET forms. FIXME...
+ return $request->getArray( $this->mName, array() );
}
}
}
function getInputHTML( $value ) {
-
- $select = parent::getInputHTML( $value );
+ $select = parent::getInputHTML( $value[1] );
$textAttribs = array(
'id' => $this->mID . '-other',
$textbox = Html::input(
$this->mName . '-other',
- '',
+ $value[2],
'text',
$textAttribs
);
return "$select<br />\n$textbox";
}
+ /**
+ * @param $request WebRequest
+ * @return Array( <overall message>, <select value>, <text field value> )
+ */
function loadDataFromRequest( $request ) {
if ( $request->getCheck( $this->mName ) ) {
$text = $request->getText( $this->mName . '-other' );
if ( $list == 'other' ) {
- return $text;
- } else {
- # Need to get the value from the key
- if( in_array( $list, $this->mFlatOptions ) ){
- $list = $this->mFlatOptions[$list];
- } else {
- # User has spoofed the select form to give an option which wasn't
- # in the original offer. Sulk...
- return $text;
- }
- }
-
- if( $text == '' ) {
- return $list;
+ $final = $text;
+ } elseif( !in_array( $list, $this->mFlatOptions ) ){
+ # User has spoofed the select form to give an option which wasn't
+ # in the original offer. Sulk...
+ $final = $text;
+ } elseif( $text == '' ) {
+ $final = $list;
} else {
- return $list . wfMsgForContent( 'colon-separator' ) . $text;
+ $final = $list . wfMsgForContent( 'colon-separator' ) . $text;
}
} else {
- return $this->getDefault();
+ $final = $this->getDefault();
+ $list = $text = '';
}
+ return array( $final, $list, $text );
}
function getSize() {
? $this->mParams['size']
: 45;
}
+
+ function validate( $value, $alldata ) {
+ # HTMLSelectField forces $value to be one of the options in the select
+ # field, which is not useful here. But we do want the validation further up
+ # the chain
+ $p = parent::validate( $value[1], $alldata );
+
+ if ( $p !== true ) {
+ return $p;
+ }
+
+ if( isset( $this->mParams['required'] ) && $value[1] === '' ){
+ return wfMsgExt( 'htmlform-required', 'parseinline' );
+ }
+
+ return true;
+ }
}
/**