3
* Object handling generic submission, CSRF protection, layout and
4
* other logic for UI forms. in a reusable manner.
6
* In order to generate the form, the HTMLForm object takes an array
7
* structure detailing the form fields available. Each element of the
8
* array is a basic property-list, including the type of field, the
9
* label it is to be given in the form, callbacks for validation and
10
* 'filtering', and other pertinent information.
12
* Field types are implemented as subclasses of the generic HTMLFormField
13
* object, and typically implement at least getInputHTML, which generates
14
* the HTML for the input field to be placed in the table.
16
* The constructor input is an associative array of $fieldname => $info,
17
* where $info is an Associative Array with any of the following:
19
* 'class' -- the subclass of HTMLFormField that will be used
20
* to create the object. *NOT* the CSS class!
21
* 'type' -- roughly translates into the <select> type attribute.
22
* if 'class' is not specified, this is used as a map
23
* through HTMLForm::$typeMappings to get the class name.
24
* 'default' -- default value when the form is displayed
25
* 'id' -- HTML id attribute
26
* 'cssclass' -- CSS class
27
* 'options' -- varies according to the specific object.
28
* 'label-message' -- message key for a message to use as the label.
29
* can be an array of msg key and then parameters to
31
* 'label' -- alternatively, a raw text message. Overridden by
33
* 'help-message' -- message key for a message to use as a help text.
34
* can be an array of msg key and then parameters to
36
* Overwrites 'help-messages'.
37
* 'help-messages' -- array of message key. As above, each item can
38
* be an array of msg key and then parameters.
39
* Overwrites 'help-message'.
40
* 'required' -- passed through to the object, indicating that it
41
* is a required field.
42
* 'size' -- the length of text fields
43
* 'filter-callback -- a function name to give you the chance to
44
* massage the inputted value before it's processed.
45
* @see HTMLForm::filter()
46
* 'validation-callback' -- a function name to give you the chance
47
* to impose extra validation on the field input.
48
* @see HTMLForm::validate()
49
* 'name' -- By default, the 'name' attribute of the input field
50
* is "wp{$fieldname}". If you want a different name
51
* (eg one without the "wp" prefix), specify it here and
52
* it will be used without modification.
54
* TODO: Document 'section' / 'subsection' stuff
56
class HTMLForm extends ContextSource {
58
// A mapping of 'type' inputs onto standard HTMLFormField subclasses
59
static $typeMappings = array(
60
'text' => 'HTMLTextField',
61
'textarea' => 'HTMLTextAreaField',
62
'select' => 'HTMLSelectField',
63
'radio' => 'HTMLRadioField',
64
'multiselect' => 'HTMLMultiSelectField',
65
'check' => 'HTMLCheckField',
66
'toggle' => 'HTMLCheckField',
67
'int' => 'HTMLIntField',
68
'float' => 'HTMLFloatField',
69
'info' => 'HTMLInfoField',
70
'selectorother' => 'HTMLSelectOrOtherField',
71
'selectandother' => 'HTMLSelectAndOtherField',
72
'submit' => 'HTMLSubmitField',
73
'hidden' => 'HTMLHiddenField',
74
'edittools' => 'HTMLEditTools',
76
// HTMLTextField will output the correct type="" attribute automagically.
77
// There are about four zillion other HTML5 input types, like url, but
78
// we don't use those at the moment, so no point in adding all of them.
79
'email' => 'HTMLTextField',
80
'password' => 'HTMLTextField',
83
protected $mMessagePrefix;
85
/** @var HTMLFormField[] */
86
protected $mFlatFields;
88
protected $mFieldTree;
89
protected $mShowReset = false;
92
protected $mSubmitCallback;
93
protected $mValidationErrorMessage;
96
protected $mHeader = '';
97
protected $mFooter = '';
98
protected $mSectionHeaders = array();
99
protected $mSectionFooters = array();
100
protected $mPost = '';
103
protected $mSubmitID;
104
protected $mSubmitName;
105
protected $mSubmitText;
106
protected $mSubmitTooltip;
109
protected $mMethod = 'post';
112
* Form action URL. false means we will use the URL to set Title
116
protected $mAction = false;
118
protected $mUseMultipart = false;
119
protected $mHiddenFields = array();
120
protected $mButtons = array();
122
protected $mWrapperLegend = false;
125
* If true, sections that contain both fields and subsections will
126
* render their subsections before their fields.
128
* Subclasses may set this to false to render subsections after fields
131
protected $mSubSectionBeforeFields = true;
134
* Build a new HTMLForm from an array of field attributes
135
* @param $descriptor Array of Field constructs, as described above
136
* @param $context IContextSource available since 1.18, will become compulsory in 1.18.
137
* Obviates the need to call $form->setTitle()
138
* @param $messagePrefix String a prefix to go in front of default messages
140
public function __construct( $descriptor, /*IContextSource*/ $context = null, $messagePrefix = '' ) {
141
if( $context instanceof IContextSource ){
142
$this->setContext( $context );
143
$this->mTitle = false; // We don't need them to set a title
144
$this->mMessagePrefix = $messagePrefix;
147
if( is_string( $context ) && $messagePrefix === '' ){
148
// it's actually $messagePrefix
149
$this->mMessagePrefix = $context;
153
// Expand out into a tree.
154
$loadedDescriptor = array();
155
$this->mFlatFields = array();
157
foreach ( $descriptor as $fieldname => $info ) {
158
$section = isset( $info['section'] )
162
if ( isset( $info['type'] ) && $info['type'] == 'file' ) {
163
$this->mUseMultipart = true;
166
$field = self::loadInputFromParameters( $fieldname, $info );
167
$field->mParent = $this;
169
$setSection =& $loadedDescriptor;
171
$sectionParts = explode( '/', $section );
173
while ( count( $sectionParts ) ) {
174
$newName = array_shift( $sectionParts );
176
if ( !isset( $setSection[$newName] ) ) {
177
$setSection[$newName] = array();
180
$setSection =& $setSection[$newName];
184
$setSection[$fieldname] = $field;
185
$this->mFlatFields[$fieldname] = $field;
188
$this->mFieldTree = $loadedDescriptor;
192
* Add the HTMLForm-specific JavaScript, if it hasn't been
194
* @deprecated since 1.18 load modules with ResourceLoader instead
196
static function addJS() { wfDeprecated( __METHOD__, '1.18' ); }
199
* Initialise a new Object for the field
200
* @param $fieldname string
201
* @param $descriptor string input Descriptor, as described above
202
* @return HTMLFormField subclass
204
static function loadInputFromParameters( $fieldname, $descriptor ) {
205
if ( isset( $descriptor['class'] ) ) {
206
$class = $descriptor['class'];
207
} elseif ( isset( $descriptor['type'] ) ) {
208
$class = self::$typeMappings[$descriptor['type']];
209
$descriptor['class'] = $class;
215
throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
218
$descriptor['fieldname'] = $fieldname;
220
$obj = new $class( $descriptor );
226
* Prepare form for submission
228
function prepareForm() {
229
# Check if we have the info we need
230
if ( !$this->mTitle instanceof Title && $this->mTitle !== false ) {
231
throw new MWException( "You must call setTitle() on an HTMLForm" );
234
# Load data from the request.
239
* Try submitting, with edit token check first
240
* @return Status|boolean
242
function tryAuthorizedSubmit() {
246
if ( $this->getMethod() != 'post' ) {
247
$submit = true; // no session check needed
248
} elseif ( $this->getRequest()->wasPosted() ) {
249
$editToken = $this->getRequest()->getVal( 'wpEditToken' );
250
if ( $this->getUser()->isLoggedIn() || $editToken != null ) {
251
// Session tokens for logged-out users have no security value.
252
// However, if the user gave one, check it in order to give a nice
253
// "session expired" error instead of "permission denied" or such.
254
$submit = $this->getUser()->matchEditToken( $editToken );
261
$result = $this->trySubmit();
268
* The here's-one-I-made-earlier option: do the submission if
269
* posted, or display the form with or without funky valiation
271
* @return Bool or Status whether submission was successful.
274
$this->prepareForm();
276
$result = $this->tryAuthorizedSubmit();
277
if ( $result === true || ( $result instanceof Status && $result->isGood() ) ){
281
$this->displayForm( $result );
286
* Validate all the fields, and call the submision callback
287
* function if everything is kosher.
288
* @return Mixed Bool true == Successful submission, Bool false
289
* == No submission attempted, anything else == Error to
292
function trySubmit() {
293
# Check for validation
294
foreach ( $this->mFlatFields as $fieldname => $field ) {
295
if ( !empty( $field->mParams['nodata'] ) ) {
298
if ( $field->validate(
299
$this->mFieldData[$fieldname],
303
return isset( $this->mValidationErrorMessage )
304
? $this->mValidationErrorMessage
305
: array( 'htmlform-invalid-input' );
309
$callback = $this->mSubmitCallback;
311
$data = $this->filterDataForSubmit( $this->mFieldData );
313
$res = call_user_func( $callback, $data, $this );
319
* Set a callback to a function to do something with the form
320
* once it's been successfully validated.
321
* @param $cb String function name. The function will be passed
322
* the output from HTMLForm::filterDataForSubmit, and must
323
* return Bool true on success, Bool false if no submission
324
* was attempted, or String HTML output to display on error.
326
function setSubmitCallback( $cb ) {
327
$this->mSubmitCallback = $cb;
331
* Set a message to display on a validation error.
332
* @param $msg Mixed String or Array of valid inputs to wfMsgExt()
333
* (so each entry can be either a String or Array)
335
function setValidationErrorMessage( $msg ) {
336
$this->mValidationErrorMessage = $msg;
340
* Set the introductory message, overwriting any existing message.
341
* @param $msg String complete text of message to display
343
function setIntro( $msg ) {
344
$this->setPreText( $msg );
348
* Set the introductory message, overwriting any existing message.
350
* @param $msg String complete text of message to display
352
function setPreText( $msg ) { $this->mPre = $msg; }
355
* Add introductory text.
356
* @param $msg String complete text of message to display
358
function addPreText( $msg ) { $this->mPre .= $msg; }
361
* Add header text, inside the form.
362
* @param $msg String complete text of message to display
363
* @param $section string The section to add the header to
365
function addHeaderText( $msg, $section = null ) {
366
if ( is_null( $section ) ) {
367
$this->mHeader .= $msg;
369
if ( !isset( $this->mSectionHeaders[$section] ) ) {
370
$this->mSectionHeaders[$section] = '';
372
$this->mSectionHeaders[$section] .= $msg;
377
* Set header text, inside the form.
379
* @param $msg String complete text of message to display
380
* @param $section The section to add the header to
382
function setHeaderText( $msg, $section = null ) {
383
if ( is_null( $section ) ) {
384
$this->mHeader = $msg;
386
$this->mSectionHeaders[$section] = $msg;
391
* Add footer text, inside the form.
392
* @param $msg String complete text of message to display
393
* @param $section string The section to add the footer text to
395
function addFooterText( $msg, $section = null ) {
396
if ( is_null( $section ) ) {
397
$this->mFooter .= $msg;
399
if ( !isset( $this->mSectionFooters[$section] ) ) {
400
$this->mSectionFooters[$section] = '';
402
$this->mSectionFooters[$section] .= $msg;
407
* Set footer text, inside the form.
409
* @param $msg String complete text of message to display
410
* @param $section string The section to add the footer text to
412
function setFooterText( $msg, $section = null ) {
413
if ( is_null( $section ) ) {
414
$this->mFooter = $msg;
416
$this->mSectionFooters[$section] = $msg;
421
* Add text to the end of the display.
422
* @param $msg String complete text of message to display
424
function addPostText( $msg ) { $this->mPost .= $msg; }
427
* Set text at the end of the display.
428
* @param $msg String complete text of message to display
430
function setPostText( $msg ) { $this->mPost = $msg; }
433
* Add a hidden field to the output
434
* @param $name String field name. This will be used exactly as entered
435
* @param $value String field value
436
* @param $attribs Array
438
public function addHiddenField( $name, $value, $attribs = array() ) {
439
$attribs += array( 'name' => $name );
440
$this->mHiddenFields[] = array( $value, $attribs );
443
public function addButton( $name, $value, $id = null, $attribs = null ) {
444
$this->mButtons[] = compact( 'name', 'value', 'id', 'attribs' );
448
* Display the form (sending to $wgOut), with an appropriate error
449
* message or stack of messages, and any validation errors, etc.
450
* @param $submitResult Mixed output from HTMLForm::trySubmit()
452
function displayForm( $submitResult ) {
453
$this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
457
* Returns the raw HTML generated by the form
458
* @param $submitResult Mixed output from HTMLForm::trySubmit()
461
function getHTML( $submitResult ) {
462
# For good measure (it is the default)
463
$this->getOutput()->preventClickjacking();
464
$this->getOutput()->addModules( 'mediawiki.htmlform' );
467
. $this->getErrors( $submitResult )
470
. $this->getHiddenFields()
471
. $this->getButtons()
475
$html = $this->wrapForm( $html );
477
return '' . $this->mPre . $html . $this->mPost;
481
* Wrap the form innards in an actual <form> element
482
* @param $html String HTML contents to wrap.
483
* @return String wrapped HTML.
485
function wrapForm( $html ) {
487
# Include a <fieldset> wrapper for style, if requested.
488
if ( $this->mWrapperLegend !== false ) {
489
$html = Xml::fieldset( $this->mWrapperLegend, $html );
491
# Use multipart/form-data
492
$encType = $this->mUseMultipart
493
? 'multipart/form-data'
494
: 'application/x-www-form-urlencoded';
497
'action' => $this->mAction === false ? $this->getTitle()->getFullURL() : $this->mAction,
498
'method' => $this->mMethod,
499
'class' => 'visualClear',
500
'enctype' => $encType,
502
if ( !empty( $this->mId ) ) {
503
$attribs['id'] = $this->mId;
506
return Html::rawElement( 'form', $attribs, $html );
510
* Get the hidden fields that should go inside the form.
511
* @return String HTML.
513
function getHiddenFields() {
514
global $wgUsePathInfo;
517
if( $this->getMethod() == 'post' ){
518
$html .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
519
$html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
522
if ( !$wgUsePathInfo && $this->getMethod() == 'get' ) {
523
$html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
526
foreach ( $this->mHiddenFields as $data ) {
527
list( $value, $attribs ) = $data;
528
$html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n";
535
* Get the submit and (potentially) reset buttons.
536
* @return String HTML.
538
function getButtons() {
542
if ( isset( $this->mSubmitID ) ) {
543
$attribs['id'] = $this->mSubmitID;
546
if ( isset( $this->mSubmitName ) ) {
547
$attribs['name'] = $this->mSubmitName;
550
if ( isset( $this->mSubmitTooltip ) ) {
551
$attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
554
$attribs['class'] = 'mw-htmlform-submit';
556
$html .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
558
if ( $this->mShowReset ) {
559
$html .= Html::element(
563
'value' => wfMsg( 'htmlform-reset' )
568
foreach ( $this->mButtons as $button ) {
571
'name' => $button['name'],
572
'value' => $button['value']
575
if ( $button['attribs'] ) {
576
$attrs += $button['attribs'];
579
if ( isset( $button['id'] ) ) {
580
$attrs['id'] = $button['id'];
583
$html .= Html::element( 'input', $attrs );
590
* Get the whole body of the form.
594
return $this->displaySection( $this->mFieldTree );
598
* Format and display an error message stack.
599
* @param $errors String|Array|Status
602
function getErrors( $errors ) {
603
if ( $errors instanceof Status ) {
604
if ( $errors->isOK() ) {
607
$errorstr = $this->getOutput()->parse( $errors->getWikiText() );
609
} elseif ( is_array( $errors ) ) {
610
$errorstr = $this->formatErrors( $errors );
616
? Html::rawElement( 'div', array( 'class' => 'error' ), $errorstr )
621
* Format a stack of error messages into a single HTML string
622
* @param $errors Array of message keys/values
623
* @return String HTML, a <ul> list of errors
625
public static function formatErrors( $errors ) {
628
foreach ( $errors as $error ) {
629
if ( is_array( $error ) ) {
630
$msg = array_shift( $error );
636
$errorstr .= Html::rawElement(
639
wfMsgExt( $msg, array( 'parseinline' ), $error )
643
$errorstr = Html::rawElement( 'ul', array(), $errorstr );
649
* Set the text for the submit button
650
* @param $t String plaintext.
652
function setSubmitText( $t ) {
653
$this->mSubmitText = $t;
657
* Set the text for the submit button to a message
659
* @param $msg String message key
661
public function setSubmitTextMsg( $msg ) {
662
return $this->setSubmitText( $this->msg( $msg )->escaped() );
666
* Get the text for the submit button, either customised or a default.
667
* @return unknown_type
669
function getSubmitText() {
670
return $this->mSubmitText
672
: wfMsg( 'htmlform-submit' );
675
public function setSubmitName( $name ) {
676
$this->mSubmitName = $name;
679
public function setSubmitTooltip( $name ) {
680
$this->mSubmitTooltip = $name;
684
* Set the id for the submit button.
686
* @todo FIXME: Integrity of $t is *not* validated
688
function setSubmitID( $t ) {
689
$this->mSubmitID = $t;
692
public function setId( $id ) {
696
* Prompt the whole form to be wrapped in a <fieldset>, with
697
* this text as its <legend> element.
698
* @param $legend String HTML to go inside the <legend> element.
701
public function setWrapperLegend( $legend ) { $this->mWrapperLegend = $legend; }
704
* Prompt the whole form to be wrapped in a <fieldset>, with
705
* this message as its <legend> element.
707
* @param $msg String message key
709
public function setWrapperLegendMsg( $msg ) {
710
return $this->setWrapperLegend( $this->msg( $msg )->escaped() );
714
* Set the prefix for various default messages
715
* TODO: currently only used for the <fieldset> legend on forms
716
* with multiple sections; should be used elsewhre?
719
function setMessagePrefix( $p ) {
720
$this->mMessagePrefix = $p;
724
* Set the title for form submission
725
* @param $t Title of page the form is on/should be posted to
727
function setTitle( $t ) {
735
function getTitle() {
736
return $this->mTitle === false
737
? $this->getContext()->getTitle()
742
* Set the method used to submit the form
743
* @param $method String
745
public function setMethod( $method='post' ){
746
$this->mMethod = $method;
749
public function getMethod(){
750
return $this->mMethod;
755
* @param $fields array[]|HTMLFormField[] array of fields (either arrays or objects)
756
* @param $sectionName string ID attribute of the <table> tag for this section, ignored if empty
757
* @param $fieldsetIDPrefix string ID prefix for the <fieldset> tag of each subsection, ignored if empty
760
function displaySection( $fields, $sectionName = '', $fieldsetIDPrefix = '' ) {
762
$subsectionHtml = '';
763
$hasLeftColumn = false;
765
foreach ( $fields as $key => $value ) {
766
if ( is_object( $value ) ) {
767
$v = empty( $value->mParams['nodata'] )
768
? $this->mFieldData[$key]
769
: $value->getDefault();
770
$tableHtml .= $value->getTableRow( $v );
772
if ( $value->getLabel() != ' ' ) {
773
$hasLeftColumn = true;
775
} elseif ( is_array( $value ) ) {
776
$section = $this->displaySection( $value, $key );
777
$legend = $this->getLegend( $key );
778
if ( isset( $this->mSectionHeaders[$key] ) ) {
779
$section = $this->mSectionHeaders[$key] . $section;
781
if ( isset( $this->mSectionFooters[$key] ) ) {
782
$section .= $this->mSectionFooters[$key];
784
$attributes = array();
785
if ( $fieldsetIDPrefix ) {
786
$attributes['id'] = Sanitizer::escapeId( "$fieldsetIDPrefix$key" );
788
$subsectionHtml .= Xml::fieldset( $legend, $section, $attributes ) . "\n";
794
if ( !$hasLeftColumn ) { // Avoid strange spacing when no labels exist
795
$classes[] = 'mw-htmlform-nolabel';
799
'class' => implode( ' ', $classes ),
802
if ( $sectionName ) {
803
$attribs['id'] = Sanitizer::escapeId( "mw-htmlform-$sectionName" );
806
$tableHtml = Html::rawElement( 'table', $attribs,
807
Html::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n";
809
if ( $this->mSubSectionBeforeFields ) {
810
return $subsectionHtml . "\n" . $tableHtml;
812
return $tableHtml . "\n" . $subsectionHtml;
817
* Construct the form fields from the Descriptor array
819
function loadData() {
820
$fieldData = array();
822
foreach ( $this->mFlatFields as $fieldname => $field ) {
823
if ( !empty( $field->mParams['nodata'] ) ) {
825
} elseif ( !empty( $field->mParams['disabled'] ) ) {
826
$fieldData[$fieldname] = $field->getDefault();
828
$fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() );
833
foreach ( $fieldData as $name => &$value ) {
834
$field = $this->mFlatFields[$name];
835
$value = $field->filter( $value, $this->mFlatFields );
838
$this->mFieldData = $fieldData;
842
* Stop a reset button being shown for this form
843
* @param $suppressReset Bool set to false to re-enable the
846
function suppressReset( $suppressReset = true ) {
847
$this->mShowReset = !$suppressReset;
851
* Overload this if you want to apply special filtration routines
852
* to the form as a whole, after it's submitted but before it's
855
* @return unknown_type
857
function filterDataForSubmit( $data ) {
862
* Get a string to go in the <legend> of a section fieldset. Override this if you
863
* want something more complicated
867
public function getLegend( $key ) {
868
return wfMsg( "{$this->mMessagePrefix}-$key" );
872
* Set the value for the action attribute of the form.
873
* When set to false (which is the default state), the set title is used.
877
* @param string|false $action
879
public function setAction( $action ) {
880
$this->mAction = $action;
886
* The parent class to generate form fields. Any field type should
887
* be a subclass of this.
889
abstract class HTMLFormField {
891
protected $mValidationCallback;
892
protected $mFilterCallback;
895
protected $mLabel; # String label. Set on construction
897
protected $mClass = '';
906
* This function must be implemented to return the HTML to generate
907
* the input object itself. It should not implement the surrounding
908
* table cells/rows, or labels/help messages.
909
* @param $value String the value to set the input to; eg a default
910
* text for a text input.
911
* @return String valid HTML.
913
abstract function getInputHTML( $value );
916
* Override this function to add specific validation checks on the
917
* field input. Don't forget to call parent::validate() to ensure
918
* that the user-defined callback mValidationCallback is still run
919
* @param $value String the value the field was submitted with
920
* @param $alldata Array the data collected from the form
921
* @return Mixed Bool true on success, or String error to display.
923
function validate( $value, $alldata ) {
924
if ( isset( $this->mParams['required'] ) && $value === '' ) {
925
return wfMsgExt( 'htmlform-required', 'parseinline' );
928
if ( isset( $this->mValidationCallback ) ) {
929
return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
935
function filter( $value, $alldata ) {
936
if ( isset( $this->mFilterCallback ) ) {
937
$value = call_user_func( $this->mFilterCallback, $value, $alldata, $this->mParent );
944
* Should this field have a label, or is there no input element with the
945
* appropriate id for the label to point to?
947
* @return bool True to output a label, false to suppress
949
protected function needsLabel() {
954
* Get the value that this input has been set to from a posted form,
955
* or the input's default value if it has not been set.
956
* @param $request WebRequest
957
* @return String the value
959
function loadDataFromRequest( $request ) {
960
if ( $request->getCheck( $this->mName ) ) {
961
return $request->getText( $this->mName );
963
return $this->getDefault();
968
* Initialise the object
969
* @param $params array Associative Array. See HTMLForm doc for syntax.
971
function __construct( $params ) {
972
$this->mParams = $params;
974
# Generate the label from a message, if possible
975
if ( isset( $params['label-message'] ) ) {
976
$msgInfo = $params['label-message'];
978
if ( is_array( $msgInfo ) ) {
979
$msg = array_shift( $msgInfo );
985
$this->mLabel = wfMsgExt( $msg, 'parseinline', $msgInfo );
986
} elseif ( isset( $params['label'] ) ) {
987
$this->mLabel = $params['label'];
990
$this->mName = "wp{$params['fieldname']}";
991
if ( isset( $params['name'] ) ) {
992
$this->mName = $params['name'];
995
$validName = Sanitizer::escapeId( $this->mName );
996
if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
997
throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
1000
$this->mID = "mw-input-{$this->mName}";
1002
if ( isset( $params['default'] ) ) {
1003
$this->mDefault = $params['default'];
1006
if ( isset( $params['id'] ) ) {
1007
$id = $params['id'];
1008
$validId = Sanitizer::escapeId( $id );
1010
if ( $id != $validId ) {
1011
throw new MWException( "Invalid id '$id' passed to " . __METHOD__ );
1017
if ( isset( $params['cssclass'] ) ) {
1018
$this->mClass = $params['cssclass'];
1021
if ( isset( $params['validation-callback'] ) ) {
1022
$this->mValidationCallback = $params['validation-callback'];
1025
if ( isset( $params['filter-callback'] ) ) {
1026
$this->mFilterCallback = $params['filter-callback'];
1029
if ( isset( $params['flatlist'] ) ){
1030
$this->mClass .= ' mw-htmlform-flatlist';
1035
* Get the complete table row for the input, including help text,
1036
* labels, and whatever.
1037
* @param $value String the value to set the input to.
1038
* @return String complete HTML table row.
1040
function getTableRow( $value ) {
1041
# Check for invalid data.
1043
$errors = $this->validate( $value, $this->mParent->mFieldData );
1045
$cellAttributes = array();
1046
$verticalLabel = false;
1048
if ( !empty($this->mParams['vertical-label']) ) {
1049
$cellAttributes['colspan'] = 2;
1050
$verticalLabel = true;
1053
if ( $errors === true || ( !$this->mParent->getRequest()->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) {
1057
$errors = self::formatErrors( $errors );
1058
$errorClass = 'mw-htmlform-invalid-input';
1061
$label = $this->getLabelHtml( $cellAttributes );
1062
$field = Html::rawElement(
1064
array( 'class' => 'mw-input' ) + $cellAttributes,
1065
$this->getInputHTML( $value ) . "\n$errors"
1068
$fieldType = get_class( $this );
1070
if ( $verticalLabel ) {
1071
$html = Html::rawElement( 'tr',
1072
array( 'class' => 'mw-htmlform-vertical-label' ), $label );
1073
$html .= Html::rawElement( 'tr',
1074
array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
1077
$html = Html::rawElement( 'tr',
1078
array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
1084
if ( isset( $this->mParams['help-message'] ) ) {
1085
$msg = wfMessage( $this->mParams['help-message'] );
1086
if ( $msg->exists() ) {
1087
$helptext = $msg->parse();
1089
} elseif ( isset( $this->mParams['help-messages'] ) ) {
1090
# help-message can be passed a message key (string) or an array containing
1091
# a message key and additional parameters. This makes it impossible to pass
1092
# an array of message key
1093
foreach( $this->mParams['help-messages'] as $name ) {
1094
$msg = wfMessage( $name );
1095
if( $msg->exists() ) {
1096
$helptext .= $msg->parse(); // append message
1099
} elseif ( isset( $this->mParams['help'] ) ) {
1100
$helptext = $this->mParams['help'];
1103
if ( !is_null( $helptext ) ) {
1104
$row = Html::rawElement( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
1106
$row = Html::rawElement( 'tr', array(), $row );
1113
function getLabel() {
1114
return $this->mLabel;
1116
function getLabelHtml( $cellAttributes = array() ) {
1117
# Don't output a for= attribute for labels with no associated input.
1118
# Kind of hacky here, possibly we don't want these to be <label>s at all.
1121
if ( $this->needsLabel() ) {
1122
$for['for'] = $this->mID;
1125
return Html::rawElement( 'td', array( 'class' => 'mw-label' ) + $cellAttributes,
1126
Html::rawElement( 'label', $for, $this->getLabel() )
1130
function getDefault() {
1131
if ( isset( $this->mDefault ) ) {
1132
return $this->mDefault;
1139
* Returns the attributes required for the tooltip and accesskey.
1141
* @return array Attributes
1143
public function getTooltipAndAccessKey() {
1144
if ( empty( $this->mParams['tooltip'] ) ) {
1147
return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
1151
* flatten an array of options to a single array, for instance,
1152
* a set of <options> inside <optgroups>.
1153
* @param $options array Associative Array with values either Strings
1155
* @return Array flattened input
1157
public static function flattenOptions( $options ) {
1158
$flatOpts = array();
1160
foreach ( $options as $value ) {
1161
if ( is_array( $value ) ) {
1162
$flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1164
$flatOpts[] = $value;
1172
* Formats one or more errors as accepted by field validation-callback.
1173
* @param $errors String|Message|Array of strings or Message instances
1174
* @return String html
1177
protected static function formatErrors( $errors ) {
1178
if ( is_array( $errors ) && count( $errors ) === 1 ) {
1179
$errors = array_shift( $errors );
1182
if ( is_array( $errors ) ) {
1184
foreach ( $errors as $error ) {
1185
if ( $error instanceof Message ) {
1186
$lines[] = Html::rawElement( 'li', array(), $error->parse() );
1188
$lines[] = Html::rawElement( 'li', array(), $error );
1191
return Html::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
1193
if ( $errors instanceof Message ) {
1194
$errors = $errors->parse();
1196
return Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
1201
class HTMLTextField extends HTMLFormField {
1202
function getSize() {
1203
return isset( $this->mParams['size'] )
1204
? $this->mParams['size']
1208
function getInputHTML( $value ) {
1211
'name' => $this->mName,
1212
'size' => $this->getSize(),
1214
) + $this->getTooltipAndAccessKey();
1216
if ( $this->mClass !== '' ) {
1217
$attribs['class'] = $this->mClass;
1220
if ( isset( $this->mParams['maxlength'] ) ) {
1221
$attribs['maxlength'] = $this->mParams['maxlength'];
1224
if ( !empty( $this->mParams['disabled'] ) ) {
1225
$attribs['disabled'] = 'disabled';
1228
# TODO: Enforce pattern, step, required, readonly on the server side as
1230
foreach ( array( 'min', 'max', 'pattern', 'title', 'step',
1231
'placeholder' ) as $param ) {
1232
if ( isset( $this->mParams[$param] ) ) {
1233
$attribs[$param] = $this->mParams[$param];
1237
foreach ( array( 'required', 'autofocus', 'multiple', 'readonly' ) as $param ) {
1238
if ( isset( $this->mParams[$param] ) ) {
1239
$attribs[$param] = '';
1243
# Implement tiny differences between some field variants
1244
# here, rather than creating a new class for each one which
1245
# is essentially just a clone of this one.
1246
if ( isset( $this->mParams['type'] ) ) {
1247
switch ( $this->mParams['type'] ) {
1249
$attribs['type'] = 'email';
1252
$attribs['type'] = 'number';
1255
$attribs['type'] = 'number';
1256
$attribs['step'] = 'any';
1261
$attribs['type'] = $this->mParams['type'];
1266
return Html::element( 'input', $attribs );
1269
class HTMLTextAreaField extends HTMLFormField {
1270
function getCols() {
1271
return isset( $this->mParams['cols'] )
1272
? $this->mParams['cols']
1276
function getRows() {
1277
return isset( $this->mParams['rows'] )
1278
? $this->mParams['rows']
1282
function getInputHTML( $value ) {
1285
'name' => $this->mName,
1286
'cols' => $this->getCols(),
1287
'rows' => $this->getRows(),
1288
) + $this->getTooltipAndAccessKey();
1290
if ( $this->mClass !== '' ) {
1291
$attribs['class'] = $this->mClass;
1294
if ( !empty( $this->mParams['disabled'] ) ) {
1295
$attribs['disabled'] = 'disabled';
1298
if ( !empty( $this->mParams['readonly'] ) ) {
1299
$attribs['readonly'] = 'readonly';
1302
foreach ( array( 'required', 'autofocus' ) as $param ) {
1303
if ( isset( $this->mParams[$param] ) ) {
1304
$attribs[$param] = '';
1308
return Html::element( 'textarea', $attribs, $value );
1313
* A field that will contain a numeric value
1315
class HTMLFloatField extends HTMLTextField {
1316
function getSize() {
1317
return isset( $this->mParams['size'] )
1318
? $this->mParams['size']
1322
function validate( $value, $alldata ) {
1323
$p = parent::validate( $value, $alldata );
1325
if ( $p !== true ) {
1329
$value = trim( $value );
1331
# http://dev.w3.org/html5/spec/common-microsyntaxes.html#real-numbers
1332
# with the addition that a leading '+' sign is ok.
1333
if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
1334
return wfMsgExt( 'htmlform-float-invalid', 'parse' );
1337
# The "int" part of these message names is rather confusing.
1338
# They make equal sense for all numbers.
1339
if ( isset( $this->mParams['min'] ) ) {
1340
$min = $this->mParams['min'];
1342
if ( $min > $value ) {
1343
return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
1347
if ( isset( $this->mParams['max'] ) ) {
1348
$max = $this->mParams['max'];
1350
if ( $max < $value ) {
1351
return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
1360
* A field that must contain a number
1362
class HTMLIntField extends HTMLFloatField {
1363
function validate( $value, $alldata ) {
1364
$p = parent::validate( $value, $alldata );
1366
if ( $p !== true ) {
1370
# http://dev.w3.org/html5/spec/common-microsyntaxes.html#signed-integers
1371
# with the addition that a leading '+' sign is ok. Note that leading zeros
1372
# are fine, and will be left in the input, which is useful for things like
1373
# phone numbers when you know that they are integers (the HTML5 type=tel
1374
# input does not require its value to be numeric). If you want a tidier
1375
# value to, eg, save in the DB, clean it up with intval().
1376
if ( !preg_match( '/^((\+|\-)?\d+)?$/', trim( $value ) )
1378
return wfMsgExt( 'htmlform-int-invalid', 'parse' );
1388
class HTMLCheckField extends HTMLFormField {
1389
function getInputHTML( $value ) {
1390
if ( !empty( $this->mParams['invert'] ) ) {
1394
$attr = $this->getTooltipAndAccessKey();
1395
$attr['id'] = $this->mID;
1397
if ( !empty( $this->mParams['disabled'] ) ) {
1398
$attr['disabled'] = 'disabled';
1401
if ( $this->mClass !== '' ) {
1402
$attr['class'] = $this->mClass;
1405
return Xml::check( $this->mName, $value, $attr ) . ' ' .
1406
Html::rawElement( 'label', array( 'for' => $this->mID ), $this->mLabel );
1410
* For a checkbox, the label goes on the right hand side, and is
1411
* added in getInputHTML(), rather than HTMLFormField::getRow()
1414
function getLabel() {
1419
* @param $request WebRequest
1422
function loadDataFromRequest( $request ) {
1424
if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
1428
// GetCheck won't work like we want for checks.
1429
// Fetch the value in either one of the two following case:
1430
// - we have a valid token (form got posted or GET forged by the user)
1431
// - checkbox name has a value (false or true), ie is not null
1432
if ( $request->getCheck( 'wpEditToken' ) || $request->getVal( $this->mName )!== null ) {
1433
// XOR has the following truth table, which is what we want
1434
// INVERT VALUE | OUTPUT
1435
// true true | false
1436
// false true | true
1437
// false false | false
1438
// true false | true
1439
return $request->getBool( $this->mName ) xor $invert;
1441
return $this->getDefault();
1447
* A select dropdown field. Basically a wrapper for Xmlselect class
1449
class HTMLSelectField extends HTMLFormField {
1450
function validate( $value, $alldata ) {
1451
$p = parent::validate( $value, $alldata );
1453
if ( $p !== true ) {
1457
$validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
1459
if ( in_array( $value, $validOptions ) )
1462
return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1465
function getInputHTML( $value ) {
1466
$select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
1468
# If one of the options' 'name' is int(0), it is automatically selected.
1469
# because PHP sucks and thinks int(0) == 'some string'.
1470
# Working around this by forcing all of them to strings.
1471
foreach( $this->mParams['options'] as &$opt ){
1472
if( is_int( $opt ) ){
1473
$opt = strval( $opt );
1476
unset( $opt ); # PHP keeps $opt around as a reference, which is a bit scary
1478
if ( !empty( $this->mParams['disabled'] ) ) {
1479
$select->setAttribute( 'disabled', 'disabled' );
1482
if ( $this->mClass !== '' ) {
1483
$select->setAttribute( 'class', $this->mClass );
1486
$select->addOptions( $this->mParams['options'] );
1488
return $select->getHTML();
1493
* Select dropdown field, with an additional "other" textbox.
1495
class HTMLSelectOrOtherField extends HTMLTextField {
1496
static $jsAdded = false;
1498
function __construct( $params ) {
1499
if ( !in_array( 'other', $params['options'], true ) ) {
1500
$msg = isset( $params['other'] ) ? $params['other'] : wfMsg( 'htmlform-selectorother-other' );
1501
$params['options'][$msg] = 'other';
1504
parent::__construct( $params );
1507
static function forceToStringRecursive( $array ) {
1508
if ( is_array( $array ) ) {
1509
return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array );
1511
return strval( $array );
1515
function getInputHTML( $value ) {
1516
$valInSelect = false;
1518
if ( $value !== false ) {
1519
$valInSelect = in_array(
1521
HTMLFormField::flattenOptions( $this->mParams['options'] )
1525
$selected = $valInSelect ? $value : 'other';
1527
$opts = self::forceToStringRecursive( $this->mParams['options'] );
1529
$select = new XmlSelect( $this->mName, $this->mID, $selected );
1530
$select->addOptions( $opts );
1532
$select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
1534
$tbAttribs = array( 'id' => $this->mID . '-other', 'size' => $this->getSize() );
1536
if ( !empty( $this->mParams['disabled'] ) ) {
1537
$select->setAttribute( 'disabled', 'disabled' );
1538
$tbAttribs['disabled'] = 'disabled';
1541
$select = $select->getHTML();
1543
if ( isset( $this->mParams['maxlength'] ) ) {
1544
$tbAttribs['maxlength'] = $this->mParams['maxlength'];
1547
if ( $this->mClass !== '' ) {
1548
$tbAttribs['class'] = $this->mClass;
1551
$textbox = Html::input(
1552
$this->mName . '-other',
1553
$valInSelect ? '' : $value,
1558
return "$select<br />\n$textbox";
1562
* @param $request WebRequest
1565
function loadDataFromRequest( $request ) {
1566
if ( $request->getCheck( $this->mName ) ) {
1567
$val = $request->getText( $this->mName );
1569
if ( $val == 'other' ) {
1570
$val = $request->getText( $this->mName . '-other' );
1575
return $this->getDefault();
1581
* Multi-select field
1583
class HTMLMultiSelectField extends HTMLFormField {
1585
function validate( $value, $alldata ) {
1586
$p = parent::validate( $value, $alldata );
1588
if ( $p !== true ) {
1592
if ( !is_array( $value ) ) {
1596
# If all options are valid, array_intersect of the valid options
1597
# and the provided options will return the provided options.
1598
$validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
1600
$validValues = array_intersect( $value, $validOptions );
1601
if ( count( $validValues ) == count( $value ) ) {
1604
return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1608
function getInputHTML( $value ) {
1609
$html = $this->formatOptions( $this->mParams['options'], $value );
1614
function formatOptions( $options, $value ) {
1619
if ( !empty( $this->mParams['disabled'] ) ) {
1620
$attribs['disabled'] = 'disabled';
1623
foreach ( $options as $label => $info ) {
1624
if ( is_array( $info ) ) {
1625
$html .= Html::rawElement( 'h1', array(), $label ) . "\n";
1626
$html .= $this->formatOptions( $info, $value );
1628
$thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
1630
$checkbox = Xml::check(
1631
$this->mName . '[]',
1632
in_array( $info, $value, true ),
1633
$attribs + $thisAttribs );
1634
$checkbox .= ' ' . Html::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
1636
$html .= ' ' . Html::rawElement( 'div', array( 'class' => 'mw-htmlform-flatlist-item' ), $checkbox );
1644
* @param $request WebRequest
1647
function loadDataFromRequest( $request ) {
1648
if ( $this->mParent->getMethod() == 'post' ) {
1649
if( $request->wasPosted() ){
1650
# Checkboxes are just not added to the request arrays if they're not checked,
1651
# so it's perfectly possible for there not to be an entry at all
1652
return $request->getArray( $this->mName, array() );
1654
# That's ok, the user has not yet submitted the form, so show the defaults
1655
return $this->getDefault();
1658
# This is the impossible case: if we look at $_GET and see no data for our
1659
# field, is it because the user has not yet submitted the form, or that they
1660
# have submitted it with all the options unchecked? We will have to assume the
1661
# latter, which basically means that you can't specify 'positive' defaults
1664
return $request->getArray( $this->mName, array() );
1668
function getDefault() {
1669
if ( isset( $this->mDefault ) ) {
1670
return $this->mDefault;
1676
protected function needsLabel() {
1682
* Double field with a dropdown list constructed from a system message in the format
1685
* * New Optgroup header
1686
* Plus a text field underneath for an additional reason. The 'value' of the field is
1687
* ""<select>: <extra reason>"", or "<extra reason>" if nothing has been selected in the
1689
* @todo FIXME: If made 'required', only the text field should be compulsory.
1691
class HTMLSelectAndOtherField extends HTMLSelectField {
1693
function __construct( $params ) {
1694
if ( array_key_exists( 'other', $params ) ) {
1695
} elseif( array_key_exists( 'other-message', $params ) ){
1696
$params['other'] = wfMessage( $params['other-message'] )->plain();
1698
$params['other'] = null;
1701
if ( array_key_exists( 'options', $params ) ) {
1702
# Options array already specified
1703
} elseif( array_key_exists( 'options-message', $params ) ){
1704
# Generate options array from a system message
1705
$params['options'] = self::parseMessage(
1706
wfMessage( $params['options-message'] )->inContentLanguage()->plain(),
1711
throw new MWException( 'HTMLSelectAndOtherField called without any options' );
1713
$this->mFlatOptions = self::flattenOptions( $params['options'] );
1715
parent::__construct( $params );
1719
* Build a drop-down box from a textual list.
1720
* @param $string String message text
1721
* @param $otherName String name of "other reason" option
1723
* TODO: this is copied from Xml::listDropDown(), deprecate/avoid duplication?
1725
public static function parseMessage( $string, $otherName=null ) {
1726
if( $otherName === null ){
1727
$otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
1731
$options = array( $otherName => 'other' );
1733
foreach ( explode( "\n", $string ) as $option ) {
1734
$value = trim( $option );
1735
if ( $value == '' ) {
1737
} elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
1738
# A new group is starting...
1739
$value = trim( substr( $value, 1 ) );
1741
} elseif ( substr( $value, 0, 2) == '**' ) {
1743
$opt = trim( substr( $value, 2 ) );
1744
if( $optgroup === false ){
1745
$options[$opt] = $opt;
1747
$options[$optgroup][$opt] = $opt;
1750
# groupless reason list
1752
$options[$option] = $option;
1759
function getInputHTML( $value ) {
1760
$select = parent::getInputHTML( $value[1] );
1762
$textAttribs = array(
1763
'id' => $this->mID . '-other',
1764
'size' => $this->getSize(),
1767
if ( $this->mClass !== '' ) {
1768
$textAttribs['class'] = $this->mClass;
1771
foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
1772
if ( isset( $this->mParams[$param] ) ) {
1773
$textAttribs[$param] = '';
1777
$textbox = Html::input(
1778
$this->mName . '-other',
1784
return "$select<br />\n$textbox";
1788
* @param $request WebRequest
1789
* @return Array( <overall message>, <select value>, <text field value> )
1791
function loadDataFromRequest( $request ) {
1792
if ( $request->getCheck( $this->mName ) ) {
1794
$list = $request->getText( $this->mName );
1795
$text = $request->getText( $this->mName . '-other' );
1797
if ( $list == 'other' ) {
1799
} elseif( !in_array( $list, $this->mFlatOptions ) ){
1800
# User has spoofed the select form to give an option which wasn't
1801
# in the original offer. Sulk...
1803
} elseif( $text == '' ) {
1806
$final = $list . wfMsgForContent( 'colon-separator' ) . $text;
1810
$final = $this->getDefault();
1814
foreach ( $this->mFlatOptions as $option ) {
1815
$match = $option . wfMsgForContent( 'colon-separator' );
1816
if( strpos( $text, $match ) === 0 ) {
1818
$text = substr( $text, strlen( $match ) );
1823
return array( $final, $list, $text );
1826
function getSize() {
1827
return isset( $this->mParams['size'] )
1828
? $this->mParams['size']
1832
function validate( $value, $alldata ) {
1833
# HTMLSelectField forces $value to be one of the options in the select
1834
# field, which is not useful here. But we do want the validation further up
1836
$p = parent::validate( $value[1], $alldata );
1838
if ( $p !== true ) {
1842
if( isset( $this->mParams['required'] ) && $value[1] === '' ){
1843
return wfMsgExt( 'htmlform-required', 'parseinline' );
1851
* Radio checkbox fields.
1853
class HTMLRadioField extends HTMLFormField {
1856
function validate( $value, $alldata ) {
1857
$p = parent::validate( $value, $alldata );
1859
if ( $p !== true ) {
1863
if ( !is_string( $value ) && !is_int( $value ) ) {
1867
$validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
1869
if ( in_array( $value, $validOptions ) ) {
1872
return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1877
* This returns a block of all the radio options, in one cell.
1878
* @see includes/HTMLFormField#getInputHTML()
1879
* @param $value String
1882
function getInputHTML( $value ) {
1883
$html = $this->formatOptions( $this->mParams['options'], $value );
1888
function formatOptions( $options, $value ) {
1892
if ( !empty( $this->mParams['disabled'] ) ) {
1893
$attribs['disabled'] = 'disabled';
1896
# TODO: should this produce an unordered list perhaps?
1897
foreach ( $options as $label => $info ) {
1898
if ( is_array( $info ) ) {
1899
$html .= Html::rawElement( 'h1', array(), $label ) . "\n";
1900
$html .= $this->formatOptions( $info, $value );
1902
$id = Sanitizer::escapeId( $this->mID . "-$info" );
1903
$radio = Xml::radio(
1907
$attribs + array( 'id' => $id )
1909
$radio .= ' ' .
1910
Html::rawElement( 'label', array( 'for' => $id ), $label );
1912
$html .= ' ' . Html::rawElement( 'div', array( 'class' => 'mw-htmlform-flatlist-item' ), $radio );
1919
protected function needsLabel() {
1925
* An information field (text blob), not a proper input.
1927
class HTMLInfoField extends HTMLFormField {
1928
function __construct( $info ) {
1929
$info['nodata'] = true;
1931
parent::__construct( $info );
1934
function getInputHTML( $value ) {
1935
return !empty( $this->mParams['raw'] ) ? $value : htmlspecialchars( $value );
1938
function getTableRow( $value ) {
1939
if ( !empty( $this->mParams['rawrow'] ) ) {
1943
return parent::getTableRow( $value );
1946
protected function needsLabel() {
1951
class HTMLHiddenField extends HTMLFormField {
1952
public function __construct( $params ) {
1953
parent::__construct( $params );
1955
# Per HTML5 spec, hidden fields cannot be 'required'
1956
# http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state
1957
unset( $this->mParams['required'] );
1960
public function getTableRow( $value ) {
1963
$params['id'] = $this->mID;
1966
$this->mParent->addHiddenField(
1975
public function getInputHTML( $value ) { return ''; }
1979
* Add a submit button inline in the form (as opposed to
1980
* HTMLForm::addButton(), which will add it at the end).
1982
class HTMLSubmitField extends HTMLFormField {
1984
function __construct( $info ) {
1985
$info['nodata'] = true;
1986
parent::__construct( $info );
1989
function getInputHTML( $value ) {
1990
return Xml::submitButton(
1993
'class' => 'mw-htmlform-submit ' . $this->mClass,
1994
'name' => $this->mName,
2000
protected function needsLabel() {
2005
* Button cannot be invalid
2006
* @param $value String
2007
* @param $alldata Array
2010
public function validate( $value, $alldata ){
2015
class HTMLEditTools extends HTMLFormField {
2016
public function getInputHTML( $value ) {
2020
public function getTableRow( $value ) {
2021
if ( empty( $this->mParams['message'] ) ) {
2022
$msg = wfMessage( 'edittools' );
2024
$msg = wfMessage( $this->mParams['message'] );
2025
if ( $msg->isDisabled() ) {
2026
$msg = wfMessage( 'edittools' );
2029
$msg->inContentLanguage();
2032
return '<tr><td></td><td class="mw-input">'
2033
. '<div class="mw-editTools">'
2034
. $msg->parseAsBlock()
2035
. "</div></td></tr>\n";