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
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;
108 protected $mContext; // <! IContextSource
110 protected $mMethod = 'post';
112 protected $mUseMultipart = false;
113 protected $mHiddenFields = array();
114 protected $mButtons = array();
116 protected $mWrapperLegend = false;
119 * Build a new HTMLForm from an array of field attributes
120 * @param $descriptor Array of Field constructs, as described above
121 * @param $context IContextSource available since 1.18, will become compulsory in 1.18.
122 * Obviates the need to call $form->setTitle()
123 * @param $messagePrefix String a prefix to go in front of default messages
125 public function __construct( $descriptor, /*IContextSource*/ $context = null, $messagePrefix = '' ) {
126 if( $context instanceof IContextSource
){
127 $this->mContext
= $context;
128 $this->mTitle
= false; // We don't need them to set a title
129 $this->mMessagePrefix
= $messagePrefix;
132 if( is_string( $context ) && $messagePrefix === '' ){
133 // it's actually $messagePrefix
134 $this->mMessagePrefix
= $context;
138 // Expand out into a tree.
139 $loadedDescriptor = array();
140 $this->mFlatFields
= array();
142 foreach ( $descriptor as $fieldname => $info ) {
143 $section = isset( $info['section'] )
147 if ( isset( $info['type'] ) && $info['type'] == 'file' ) {
148 $this->mUseMultipart
= true;
151 $field = self
::loadInputFromParameters( $fieldname, $info );
152 $field->mParent
= $this;
154 $setSection =& $loadedDescriptor;
156 $sectionParts = explode( '/', $section );
158 while ( count( $sectionParts ) ) {
159 $newName = array_shift( $sectionParts );
161 if ( !isset( $setSection[$newName] ) ) {
162 $setSection[$newName] = array();
165 $setSection =& $setSection[$newName];
169 $setSection[$fieldname] = $field;
170 $this->mFlatFields
[$fieldname] = $field;
173 $this->mFieldTree
= $loadedDescriptor;
177 * Add the HTMLForm-specific JavaScript, if it hasn't been
179 * @deprecated since 1.18 load modules with ResourceLoader instead
181 static function addJS() { }
184 * Initialise a new Object for the field
185 * @param $fieldname string
186 * @param $descriptor string input Descriptor, as described above
187 * @return HTMLFormField subclass
189 static function loadInputFromParameters( $fieldname, $descriptor ) {
190 if ( isset( $descriptor['class'] ) ) {
191 $class = $descriptor['class'];
192 } elseif ( isset( $descriptor['type'] ) ) {
193 $class = self
::$typeMappings[$descriptor['type']];
194 $descriptor['class'] = $class;
200 throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
203 $descriptor['fieldname'] = $fieldname;
205 $obj = new $class( $descriptor );
211 * Prepare form for submission
213 function prepareForm() {
214 # Check if we have the info we need
215 if ( !$this->mTitle
instanceof Title
&& $this->mTitle
!== false ) {
216 throw new MWException( "You must call setTitle() on an HTMLForm" );
219 # Load data from the request.
224 * Try submitting, with edit token check first
225 * @return Status|boolean
227 function tryAuthorizedSubmit() {
228 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
231 if ( $this->getMethod() != 'post' ||
$this->getUser()->matchEditToken( $editToken ) ) {
232 $result = $this->trySubmit();
238 * The here's-one-I-made-earlier option: do the submission if
239 * posted, or display the form with or without funky valiation
241 * @return Bool or Status whether submission was successful.
244 $this->prepareForm();
246 $result = $this->tryAuthorizedSubmit();
247 if ( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ){
251 $this->displayForm( $result );
256 * Validate all the fields, and call the submision callback
257 * function if everything is kosher.
258 * @return Mixed Bool true == Successful submission, Bool false
259 * == No submission attempted, anything else == Error to
262 function trySubmit() {
263 # Check for validation
264 foreach ( $this->mFlatFields
as $fieldname => $field ) {
265 if ( !empty( $field->mParams
['nodata'] ) ) {
268 if ( $field->validate(
269 $this->mFieldData
[$fieldname],
273 return isset( $this->mValidationErrorMessage
)
274 ?
$this->mValidationErrorMessage
275 : array( 'htmlform-invalid-input' );
279 $callback = $this->mSubmitCallback
;
281 $data = $this->filterDataForSubmit( $this->mFieldData
);
283 $res = call_user_func( $callback, $data, $this );
289 * Set a callback to a function to do something with the form
290 * once it's been successfully validated.
291 * @param $cb String function name. The function will be passed
292 * the output from HTMLForm::filterDataForSubmit, and must
293 * return Bool true on success, Bool false if no submission
294 * was attempted, or String HTML output to display on error.
296 function setSubmitCallback( $cb ) {
297 $this->mSubmitCallback
= $cb;
301 * Set a message to display on a validation error.
302 * @param $msg Mixed String or Array of valid inputs to wfMsgExt()
303 * (so each entry can be either a String or Array)
305 function setValidationErrorMessage( $msg ) {
306 $this->mValidationErrorMessage
= $msg;
310 * Set the introductory message, overwriting any existing message.
311 * @param $msg String complete text of message to display
313 function setIntro( $msg ) { $this->mPre
= $msg; }
316 * Add introductory text.
317 * @param $msg String complete text of message to display
319 function addPreText( $msg ) { $this->mPre
.= $msg; }
322 * Add header text, inside the form.
323 * @param $msg String complete text of message to display
324 * @param $section The section to add the header to
326 function addHeaderText( $msg, $section = null ) {
327 if ( is_null( $section ) ) {
328 $this->mHeader
.= $msg;
330 if ( !isset( $this->mSectionHeaders
[$section] ) ) {
331 $this->mSectionHeaders
[$section] = '';
333 $this->mSectionHeaders
[$section] .= $msg;
338 * Add footer text, inside the form.
339 * @param $msg String complete text of message to display
340 * @param $section string The section to add the footer text to
342 function addFooterText( $msg, $section = null ) {
343 if ( is_null( $section ) ) {
344 $this->mFooter
.= $msg;
346 if ( !isset( $this->mSectionFooters
[$section] ) ) {
347 $this->mSectionFooters
[$section] = '';
349 $this->mSectionFooters
[$section] .= $msg;
354 * Add text to the end of the display.
355 * @param $msg String complete text of message to display
357 function addPostText( $msg ) { $this->mPost
.= $msg; }
360 * Add a hidden field to the output
361 * @param $name String field name. This will be used exactly as entered
362 * @param $value String field value
363 * @param $attribs Array
365 public function addHiddenField( $name, $value, $attribs = array() ) {
366 $attribs +
= array( 'name' => $name );
367 $this->mHiddenFields
[] = array( $value, $attribs );
370 public function addButton( $name, $value, $id = null, $attribs = null ) {
371 $this->mButtons
[] = compact( 'name', 'value', 'id', 'attribs' );
375 * Display the form (sending to wgOut), with an appropriate error
376 * message or stack of messages, and any validation errors, etc.
377 * @param $submitResult Mixed output from HTMLForm::trySubmit()
379 function displayForm( $submitResult ) {
380 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
384 * Returns the raw HTML generated by the form
385 * @param $submitResult Mixed output from HTMLForm::trySubmit()
388 function getHTML( $submitResult ) {
389 # For good measure (it is the default)
390 $this->getOutput()->preventClickjacking();
391 $this->getOutput()->addModules( 'mediawiki.htmlform' );
394 . $this->getErrors( $submitResult )
397 . $this->getHiddenFields()
398 . $this->getButtons()
402 $html = $this->wrapForm( $html );
404 return '' . $this->mPre
. $html . $this->mPost
;
408 * Wrap the form innards in an actual <form> element
409 * @param $html String HTML contents to wrap.
410 * @return String wrapped HTML.
412 function wrapForm( $html ) {
414 # Include a <fieldset> wrapper for style, if requested.
415 if ( $this->mWrapperLegend
!== false ) {
416 $html = Xml
::fieldset( $this->mWrapperLegend
, $html );
418 # Use multipart/form-data
419 $encType = $this->mUseMultipart
420 ?
'multipart/form-data'
421 : 'application/x-www-form-urlencoded';
424 'action' => $this->getTitle()->getFullURL(),
425 'method' => $this->mMethod
,
426 'class' => 'visualClear',
427 'enctype' => $encType,
429 if ( !empty( $this->mId
) ) {
430 $attribs['id'] = $this->mId
;
433 return Html
::rawElement( 'form', $attribs, $html );
437 * Get the hidden fields that should go inside the form.
438 * @return String HTML.
440 function getHiddenFields() {
441 global $wgUsePathInfo;
444 if( $this->getMethod() == 'post' ){
445 $html .= Html
::hidden( 'wpEditToken', $this->getUser()->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
446 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
449 if ( !$wgUsePathInfo && $this->getMethod() == 'get' ) {
450 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
453 foreach ( $this->mHiddenFields
as $data ) {
454 list( $value, $attribs ) = $data;
455 $html .= Html
::hidden( $attribs['name'], $value, $attribs ) . "\n";
462 * Get the submit and (potentially) reset buttons.
463 * @return String HTML.
465 function getButtons() {
469 if ( isset( $this->mSubmitID
) ) {
470 $attribs['id'] = $this->mSubmitID
;
473 if ( isset( $this->mSubmitName
) ) {
474 $attribs['name'] = $this->mSubmitName
;
477 if ( isset( $this->mSubmitTooltip
) ) {
478 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
481 $attribs['class'] = 'mw-htmlform-submit';
483 $html .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
485 if ( $this->mShowReset
) {
486 $html .= Html
::element(
490 'value' => wfMsg( 'htmlform-reset' )
495 foreach ( $this->mButtons
as $button ) {
498 'name' => $button['name'],
499 'value' => $button['value']
502 if ( $button['attribs'] ) {
503 $attrs +
= $button['attribs'];
506 if ( isset( $button['id'] ) ) {
507 $attrs['id'] = $button['id'];
510 $html .= Html
::element( 'input', $attrs );
517 * Get the whole body of the form.
521 return $this->displaySection( $this->mFieldTree
);
525 * Format and display an error message stack.
526 * @param $errors String|Array|Status
529 function getErrors( $errors ) {
530 if ( $errors instanceof Status
) {
531 if ( $errors->isOK() ) {
534 $errorstr = $this->getOutput()->parse( $errors->getWikiText() );
536 } elseif ( is_array( $errors ) ) {
537 $errorstr = $this->formatErrors( $errors );
543 ? Html
::rawElement( 'div', array( 'class' => 'error' ), $errorstr )
548 * Format a stack of error messages into a single HTML string
549 * @param $errors Array of message keys/values
550 * @return String HTML, a <ul> list of errors
552 public static function formatErrors( $errors ) {
555 foreach ( $errors as $error ) {
556 if ( is_array( $error ) ) {
557 $msg = array_shift( $error );
563 $errorstr .= Html
::rawElement(
566 wfMsgExt( $msg, array( 'parseinline' ), $error )
570 $errorstr = Html
::rawElement( 'ul', array(), $errorstr );
576 * Set the text for the submit button
577 * @param $t String plaintext.
579 function setSubmitText( $t ) {
580 $this->mSubmitText
= $t;
584 * Set the text for the submit button to a message
585 * @param $msg String message key
587 public function setSubmitTextMsg( $msg ) {
588 return $this->setSubmitText( wfMsg( $msg ) );
592 * Get the text for the submit button, either customised or a default.
593 * @return unknown_type
595 function getSubmitText() {
596 return $this->mSubmitText
598 : wfMsg( 'htmlform-submit' );
601 public function setSubmitName( $name ) {
602 $this->mSubmitName
= $name;
605 public function setSubmitTooltip( $name ) {
606 $this->mSubmitTooltip
= $name;
610 * Set the id for the submit button.
612 * @todo FIXME: Integrity of $t is *not* validated
614 function setSubmitID( $t ) {
615 $this->mSubmitID
= $t;
618 public function setId( $id ) {
622 * Prompt the whole form to be wrapped in a <fieldset>, with
623 * this text as its <legend> element.
624 * @param $legend String HTML to go inside the <legend> element.
627 public function setWrapperLegend( $legend ) { $this->mWrapperLegend
= $legend; }
630 * Prompt the whole form to be wrapped in a <fieldset>, with
631 * this message as its <legend> element.
632 * @param $msg String message key
634 public function setWrapperLegendMsg( $msg ) {
635 return $this->setWrapperLegend( wfMsg( $msg ) );
639 * Set the prefix for various default messages
640 * TODO: currently only used for the <fieldset> legend on forms
641 * with multiple sections; should be used elsewhre?
644 function setMessagePrefix( $p ) {
645 $this->mMessagePrefix
= $p;
649 * Set the title for form submission
650 * @param $t Title of page the form is on/should be posted to
652 function setTitle( $t ) {
660 function getTitle() {
661 return $this->mTitle
=== false
662 ?
$this->getContext()->getTitle()
667 * @return IContextSource
669 public function getContext(){
670 return $this->mContext
instanceof IContextSource
672 : RequestContext
::getMain();
678 public function getOutput(){
679 return $this->getContext()->getOutput();
685 public function getRequest(){
686 return $this->getContext()->getRequest();
692 public function getUser(){
693 return $this->getContext()->getUser();
697 * Set the method used to submit the form
698 * @param $method String
700 public function setMethod( $method='post' ){
701 $this->mMethod
= $method;
704 public function getMethod(){
705 return $this->mMethod
;
710 * @param $fields array[]|HTMLFormField[] array of fields (either arrays or objects)
711 * @param $sectionName string ID attribute of the <table> tag for this section, ignored if empty
712 * @param $fieldsetIDPrefix string ID prefix for the <fieldset> tag of each subsection, ignored if empty
715 function displaySection( $fields, $sectionName = '', $fieldsetIDPrefix = '' ) {
717 $subsectionHtml = '';
718 $hasLeftColumn = false;
720 foreach ( $fields as $key => $value ) {
721 if ( is_object( $value ) ) {
722 $v = empty( $value->mParams
['nodata'] )
723 ?
$this->mFieldData
[$key]
724 : $value->getDefault();
725 $tableHtml .= $value->getTableRow( $v );
727 if ( $value->getLabel() != ' ' ) {
728 $hasLeftColumn = true;
730 } elseif ( is_array( $value ) ) {
731 $section = $this->displaySection( $value, $key );
732 $legend = $this->getLegend( $key );
733 if ( isset( $this->mSectionHeaders
[$key] ) ) {
734 $section = $this->mSectionHeaders
[$key] . $section;
736 if ( isset( $this->mSectionFooters
[$key] ) ) {
737 $section .= $this->mSectionFooters
[$key];
739 $attributes = array();
740 if ( $fieldsetIDPrefix ) {
741 $attributes['id'] = Sanitizer
::escapeId( "$fieldsetIDPrefix$key" );
743 $subsectionHtml .= Xml
::fieldset( $legend, $section, $attributes ) . "\n";
749 if ( !$hasLeftColumn ) { // Avoid strange spacing when no labels exist
750 $classes[] = 'mw-htmlform-nolabel';
754 'class' => implode( ' ', $classes ),
757 if ( $sectionName ) {
758 $attribs['id'] = Sanitizer
::escapeId( "mw-htmlform-$sectionName" );
761 $tableHtml = Html
::rawElement( 'table', $attribs,
762 Html
::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n";
764 return $subsectionHtml . "\n" . $tableHtml;
768 * Construct the form fields from the Descriptor array
770 function loadData() {
771 $fieldData = array();
773 foreach ( $this->mFlatFields
as $fieldname => $field ) {
774 if ( !empty( $field->mParams
['nodata'] ) ) {
776 } elseif ( !empty( $field->mParams
['disabled'] ) ) {
777 $fieldData[$fieldname] = $field->getDefault();
779 $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() );
784 foreach ( $fieldData as $name => &$value ) {
785 $field = $this->mFlatFields
[$name];
786 $value = $field->filter( $value, $this->mFlatFields
);
789 $this->mFieldData
= $fieldData;
793 * Stop a reset button being shown for this form
794 * @param $suppressReset Bool set to false to re-enable the
797 function suppressReset( $suppressReset = true ) {
798 $this->mShowReset
= !$suppressReset;
802 * Overload this if you want to apply special filtration routines
803 * to the form as a whole, after it's submitted but before it's
806 * @return unknown_type
808 function filterDataForSubmit( $data ) {
813 * Get a string to go in the <legend> of a section fieldset. Override this if you
814 * want something more complicated
818 public function getLegend( $key ) {
819 return wfMsg( "{$this->mMessagePrefix}-$key" );
824 * The parent class to generate form fields. Any field type should
825 * be a subclass of this.
827 abstract class HTMLFormField
{
829 protected $mValidationCallback;
830 protected $mFilterCallback;
833 protected $mLabel; # String label. Set on construction
835 protected $mClass = '';
844 * This function must be implemented to return the HTML to generate
845 * the input object itself. It should not implement the surrounding
846 * table cells/rows, or labels/help messages.
847 * @param $value String the value to set the input to; eg a default
848 * text for a text input.
849 * @return String valid HTML.
851 abstract function getInputHTML( $value );
854 * Override this function to add specific validation checks on the
855 * field input. Don't forget to call parent::validate() to ensure
856 * that the user-defined callback mValidationCallback is still run
857 * @param $value String the value the field was submitted with
858 * @param $alldata Array the data collected from the form
859 * @return Mixed Bool true on success, or String error to display.
861 function validate( $value, $alldata ) {
862 if ( isset( $this->mParams
['required'] ) && $value === '' ) {
863 return wfMsgExt( 'htmlform-required', 'parseinline' );
866 if ( isset( $this->mValidationCallback
) ) {
867 return call_user_func( $this->mValidationCallback
, $value, $alldata );
873 function filter( $value, $alldata ) {
874 if ( isset( $this->mFilterCallback
) ) {
875 $value = call_user_func( $this->mFilterCallback
, $value, $alldata );
882 * Should this field have a label, or is there no input element with the
883 * appropriate id for the label to point to?
885 * @return bool True to output a label, false to suppress
887 protected function needsLabel() {
892 * Get the value that this input has been set to from a posted form,
893 * or the input's default value if it has not been set.
894 * @param $request WebRequest
895 * @return String the value
897 function loadDataFromRequest( $request ) {
898 if ( $request->getCheck( $this->mName
) ) {
899 return $request->getText( $this->mName
);
901 return $this->getDefault();
906 * Initialise the object
907 * @param $params array Associative Array. See HTMLForm doc for syntax.
909 function __construct( $params ) {
910 $this->mParams
= $params;
912 # Generate the label from a message, if possible
913 if ( isset( $params['label-message'] ) ) {
914 $msgInfo = $params['label-message'];
916 if ( is_array( $msgInfo ) ) {
917 $msg = array_shift( $msgInfo );
923 $this->mLabel
= wfMsgExt( $msg, 'parseinline', $msgInfo );
924 } elseif ( isset( $params['label'] ) ) {
925 $this->mLabel
= $params['label'];
928 $this->mName
= "wp{$params['fieldname']}";
929 if ( isset( $params['name'] ) ) {
930 $this->mName
= $params['name'];
933 $validName = Sanitizer
::escapeId( $this->mName
);
934 if ( $this->mName
!= $validName && !isset( $params['nodata'] ) ) {
935 throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__
);
938 $this->mID
= "mw-input-{$this->mName}";
940 if ( isset( $params['default'] ) ) {
941 $this->mDefault
= $params['default'];
944 if ( isset( $params['id'] ) ) {
946 $validId = Sanitizer
::escapeId( $id );
948 if ( $id != $validId ) {
949 throw new MWException( "Invalid id '$id' passed to " . __METHOD__
);
955 if ( isset( $params['cssclass'] ) ) {
956 $this->mClass
= $params['cssclass'];
959 if ( isset( $params['validation-callback'] ) ) {
960 $this->mValidationCallback
= $params['validation-callback'];
963 if ( isset( $params['filter-callback'] ) ) {
964 $this->mFilterCallback
= $params['filter-callback'];
969 * Get the complete table row for the input, including help text,
970 * labels, and whatever.
971 * @param $value String the value to set the input to.
972 * @return String complete HTML table row.
974 function getTableRow( $value ) {
975 # Check for invalid data.
977 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
979 $cellAttributes = array();
980 $verticalLabel = false;
982 if ( !empty($this->mParams
['vertical-label']) ) {
983 $cellAttributes['colspan'] = 2;
984 $verticalLabel = true;
987 if ( $errors === true ||
( !$this->mParent
->getRequest()->wasPosted() && ( $this->mParent
->getMethod() == 'post' ) ) ) {
991 $errors = self
::formatErrors( $errors );
992 $errorClass = 'mw-htmlform-invalid-input';
995 $label = $this->getLabelHtml( $cellAttributes );
996 $field = Html
::rawElement(
998 array( 'class' => 'mw-input' ) +
$cellAttributes,
999 $this->getInputHTML( $value ) . "\n$errors"
1002 $fieldType = get_class( $this );
1004 if ( $verticalLabel ) {
1005 $html = Html
::rawElement( 'tr',
1006 array( 'class' => 'mw-htmlform-vertical-label' ), $label );
1007 $html .= Html
::rawElement( 'tr',
1008 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
1011 $html = Html
::rawElement( 'tr',
1012 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
1018 if ( isset( $this->mParams
['help-message'] ) ) {
1019 $msg = wfMessage( $this->mParams
['help-message'] );
1020 if ( $msg->exists() ) {
1021 $helptext = $msg->parse();
1023 } elseif ( isset( $this->mParams
['help-messages'] ) ) {
1024 # help-message can be passed a message key (string) or an array containing
1025 # a message key and additional parameters. This makes it impossible to pass
1026 # an array of message key
1027 foreach( $this->mParams
['help-messages'] as $name ) {
1028 $msg = wfMessage( $name );
1029 if( $msg->exists() ) {
1030 $helptext .= $msg->parse(); // append message
1033 } elseif ( isset( $this->mParams
['help'] ) ) {
1034 $helptext = $this->mParams
['help'];
1037 if ( !is_null( $helptext ) ) {
1038 $row = Html
::rawElement( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
1040 $row = Html
::rawElement( 'tr', array(), $row );
1047 function getLabel() {
1048 return $this->mLabel
;
1050 function getLabelHtml( $cellAttributes = array() ) {
1051 # Don't output a for= attribute for labels with no associated input.
1052 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1055 if ( $this->needsLabel() ) {
1056 $for['for'] = $this->mID
;
1059 return Html
::rawElement( 'td', array( 'class' => 'mw-label' ) +
$cellAttributes,
1060 Html
::rawElement( 'label', $for, $this->getLabel() )
1064 function getDefault() {
1065 if ( isset( $this->mDefault
) ) {
1066 return $this->mDefault
;
1073 * Returns the attributes required for the tooltip and accesskey.
1075 * @return array Attributes
1077 public function getTooltipAndAccessKey() {
1078 if ( empty( $this->mParams
['tooltip'] ) ) {
1081 return Linker
::tooltipAndAccesskeyAttribs( $this->mParams
['tooltip'] );
1085 * flatten an array of options to a single array, for instance,
1086 * a set of <options> inside <optgroups>.
1087 * @param $options Associative Array with values either Strings
1089 * @return Array flattened input
1091 public static function flattenOptions( $options ) {
1092 $flatOpts = array();
1094 foreach ( $options as $value ) {
1095 if ( is_array( $value ) ) {
1096 $flatOpts = array_merge( $flatOpts, self
::flattenOptions( $value ) );
1098 $flatOpts[] = $value;
1106 * Formats one or more errors as accepted by field validation-callback.
1107 * @param $errors String|Message|Array of strings or Message instances
1108 * @return String html
1111 protected static function formatErrors( $errors ) {
1112 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1113 $errors = array_shift( $errors );
1116 if ( is_array( $errors ) ) {
1118 foreach ( $errors as $error ) {
1119 if ( $error instanceof Message
) {
1120 $lines[] = Html
::rawElement( 'li', array(), $error->parse() );
1122 $lines[] = Html
::rawElement( 'li', array(), $error );
1125 return Html
::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
1127 if ( $errors instanceof Message
) {
1128 $errors = $errors->parse();
1130 return Html
::rawElement( 'span', array( 'class' => 'error' ), $errors );
1135 class HTMLTextField
extends HTMLFormField
{
1136 function getSize() {
1137 return isset( $this->mParams
['size'] )
1138 ?
$this->mParams
['size']
1142 function getInputHTML( $value ) {
1145 'name' => $this->mName
,
1146 'size' => $this->getSize(),
1148 ) +
$this->getTooltipAndAccessKey();
1150 if ( isset( $this->mParams
['maxlength'] ) ) {
1151 $attribs['maxlength'] = $this->mParams
['maxlength'];
1154 if ( !empty( $this->mParams
['disabled'] ) ) {
1155 $attribs['disabled'] = 'disabled';
1158 # TODO: Enforce pattern, step, required, readonly on the server side as
1160 foreach ( array( 'min', 'max', 'pattern', 'title', 'step',
1161 'placeholder' ) as $param ) {
1162 if ( isset( $this->mParams
[$param] ) ) {
1163 $attribs[$param] = $this->mParams
[$param];
1167 foreach ( array( 'required', 'autofocus', 'multiple', 'readonly' ) as $param ) {
1168 if ( isset( $this->mParams
[$param] ) ) {
1169 $attribs[$param] = '';
1173 # Implement tiny differences between some field variants
1174 # here, rather than creating a new class for each one which
1175 # is essentially just a clone of this one.
1176 if ( isset( $this->mParams
['type'] ) ) {
1177 switch ( $this->mParams
['type'] ) {
1179 $attribs['type'] = 'email';
1182 $attribs['type'] = 'number';
1185 $attribs['type'] = 'number';
1186 $attribs['step'] = 'any';
1191 $attribs['type'] = $this->mParams
['type'];
1196 return Html
::element( 'input', $attribs );
1199 class HTMLTextAreaField
extends HTMLFormField
{
1200 function getCols() {
1201 return isset( $this->mParams
['cols'] )
1202 ?
$this->mParams
['cols']
1206 function getRows() {
1207 return isset( $this->mParams
['rows'] )
1208 ?
$this->mParams
['rows']
1212 function getInputHTML( $value ) {
1215 'name' => $this->mName
,
1216 'cols' => $this->getCols(),
1217 'rows' => $this->getRows(),
1218 ) +
$this->getTooltipAndAccessKey();
1221 if ( !empty( $this->mParams
['disabled'] ) ) {
1222 $attribs['disabled'] = 'disabled';
1225 if ( !empty( $this->mParams
['readonly'] ) ) {
1226 $attribs['readonly'] = 'readonly';
1229 foreach ( array( 'required', 'autofocus' ) as $param ) {
1230 if ( isset( $this->mParams
[$param] ) ) {
1231 $attribs[$param] = '';
1235 return Html
::element( 'textarea', $attribs, $value );
1240 * A field that will contain a numeric value
1242 class HTMLFloatField
extends HTMLTextField
{
1243 function getSize() {
1244 return isset( $this->mParams
['size'] )
1245 ?
$this->mParams
['size']
1249 function validate( $value, $alldata ) {
1250 $p = parent
::validate( $value, $alldata );
1252 if ( $p !== true ) {
1256 $value = trim( $value );
1258 # http://dev.w3.org/html5/spec/common-microsyntaxes.html#real-numbers
1259 # with the addition that a leading '+' sign is ok.
1260 if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
1261 return wfMsgExt( 'htmlform-float-invalid', 'parse' );
1264 # The "int" part of these message names is rather confusing.
1265 # They make equal sense for all numbers.
1266 if ( isset( $this->mParams
['min'] ) ) {
1267 $min = $this->mParams
['min'];
1269 if ( $min > $value ) {
1270 return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
1274 if ( isset( $this->mParams
['max'] ) ) {
1275 $max = $this->mParams
['max'];
1277 if ( $max < $value ) {
1278 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
1287 * A field that must contain a number
1289 class HTMLIntField
extends HTMLFloatField
{
1290 function validate( $value, $alldata ) {
1291 $p = parent
::validate( $value, $alldata );
1293 if ( $p !== true ) {
1297 # http://dev.w3.org/html5/spec/common-microsyntaxes.html#signed-integers
1298 # with the addition that a leading '+' sign is ok. Note that leading zeros
1299 # are fine, and will be left in the input, which is useful for things like
1300 # phone numbers when you know that they are integers (the HTML5 type=tel
1301 # input does not require its value to be numeric). If you want a tidier
1302 # value to, eg, save in the DB, clean it up with intval().
1303 if ( !preg_match( '/^((\+|\-)?\d+)?$/', trim( $value ) )
1305 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
1315 class HTMLCheckField
extends HTMLFormField
{
1316 function getInputHTML( $value ) {
1317 if ( !empty( $this->mParams
['invert'] ) ) {
1321 $attr = $this->getTooltipAndAccessKey();
1322 $attr['id'] = $this->mID
;
1324 if ( !empty( $this->mParams
['disabled'] ) ) {
1325 $attr['disabled'] = 'disabled';
1328 return Xml
::check( $this->mName
, $value, $attr ) . ' ' .
1329 Html
::rawElement( 'label', array( 'for' => $this->mID
), $this->mLabel
);
1333 * For a checkbox, the label goes on the right hand side, and is
1334 * added in getInputHTML(), rather than HTMLFormField::getRow()
1337 function getLabel() {
1342 * @param $request WebRequest
1345 function loadDataFromRequest( $request ) {
1347 if ( isset( $this->mParams
['invert'] ) && $this->mParams
['invert'] ) {
1351 // GetCheck won't work like we want for checks.
1352 if ( $request->getCheck( 'wpEditToken' ) ||
$this->mParent
->getMethod() != 'post' ) {
1353 // XOR has the following truth table, which is what we want
1354 // INVERT VALUE | OUTPUT
1355 // true true | false
1356 // false true | true
1357 // false false | false
1358 // true false | true
1359 return $request->getBool( $this->mName
) xor $invert;
1361 return $this->getDefault();
1367 * A select dropdown field. Basically a wrapper for Xmlselect class
1369 class HTMLSelectField
extends HTMLFormField
{
1370 function validate( $value, $alldata ) {
1371 $p = parent
::validate( $value, $alldata );
1373 if ( $p !== true ) {
1379 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
1381 if ( in_array( $value, $validOptions ) )
1384 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1387 function getInputHTML( $value ) {
1388 $select = new XmlSelect( $this->mName
, $this->mID
, strval( $value ) );
1390 # If one of the options' 'name' is int(0), it is automatically selected.
1391 # because PHP sucks and thinks int(0) == 'some string'.
1392 # Working around this by forcing all of them to strings.
1393 foreach( $this->mParams
['options'] as &$opt ){
1394 if( is_int( $opt ) ){
1395 $opt = strval( $opt );
1398 unset( $opt ); # PHP keeps $opt around as a reference, which is a bit scary
1400 if ( !empty( $this->mParams
['disabled'] ) ) {
1401 $select->setAttribute( 'disabled', 'disabled' );
1404 if ( !empty( $this->mParams
['multiple'] ) ) {
1405 $select->setAttribute( 'name', $this->mName
. '[]' );
1406 $select->setAttribute( 'multiple', 'multiple' );
1408 if ( !empty( $this->mParams
['size'] ) ) {
1409 $select->setAttribute( 'size', $this->mParams
['size'] );
1413 $select->addOptions( $this->mParams
['options'] );
1415 return $select->getHTML();
1419 * @param $request WebRequest
1422 function loadDataFromRequest( $request ) {
1423 if ( $this->mParent
->getMethod() == 'post' ) {
1424 if( $request->wasPosted() ){
1425 # Checkboxes are just not added to the request arrays if they're not checked,
1426 # so it's perfectly possible for there not to be an entry at all
1427 return $request->getArray( $this->mName
, array() );
1429 # That's ok, the user has not yet submitted the form, so show the defaults
1430 return $this->getDefault();
1433 # This is the impossible case: if we look at $_GET and see no data for our
1434 # field, is it because the user has not yet submitted the form, or that they
1435 # have submitted it with all the options unchecked? We will have to assume the
1436 # latter, which basically means that you can't specify 'positive' defaults
1439 return $request->getArray( $this->mName
, array() );
1443 public static function keysAreValues( $array ) {
1444 $resultArray = array();
1446 foreach ( $array as $name => $value ) {
1447 $resultArray[$value] = $value;
1450 return $resultArray;
1455 * Select dropdown field, with an additional "other" textbox.
1457 class HTMLSelectOrOtherField
extends HTMLTextField
{
1458 static $jsAdded = false;
1460 function __construct( $params ) {
1461 if ( !in_array( 'other', $params['options'], true ) ) {
1462 $msg = isset( $params['other'] ) ?
$params['other'] : wfMsg( 'htmlform-selectorother-other' );
1463 $params['options'][$msg] = 'other';
1466 parent
::__construct( $params );
1469 static function forceToStringRecursive( $array ) {
1470 if ( is_array( $array ) ) {
1471 return array_map( array( __CLASS__
, 'forceToStringRecursive' ), $array );
1473 return strval( $array );
1477 function getInputHTML( $value ) {
1478 $valInSelect = false;
1480 if ( $value !== false ) {
1481 $valInSelect = in_array(
1483 HTMLFormField
::flattenOptions( $this->mParams
['options'] )
1487 $selected = $valInSelect ?
$value : 'other';
1489 $opts = self
::forceToStringRecursive( $this->mParams
['options'] );
1491 $select = new XmlSelect( $this->mName
, $this->mID
, $selected );
1492 $select->addOptions( $opts );
1494 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
1496 $tbAttribs = array( 'id' => $this->mID
. '-other', 'size' => $this->getSize() );
1498 if ( !empty( $this->mParams
['disabled'] ) ) {
1499 $select->setAttribute( 'disabled', 'disabled' );
1500 $tbAttribs['disabled'] = 'disabled';
1503 $select = $select->getHTML();
1505 if ( isset( $this->mParams
['maxlength'] ) ) {
1506 $tbAttribs['maxlength'] = $this->mParams
['maxlength'];
1509 $textbox = Html
::input(
1510 $this->mName
. '-other',
1511 $valInSelect ?
'' : $value,
1516 return "$select<br />\n$textbox";
1520 * @param $request WebRequest
1523 function loadDataFromRequest( $request ) {
1524 if ( $request->getCheck( $this->mName
) ) {
1525 $val = $request->getText( $this->mName
);
1527 if ( $val == 'other' ) {
1528 $val = $request->getText( $this->mName
. '-other' );
1533 return $this->getDefault();
1539 * Multi-select field
1541 class HTMLMultiSelectField
extends HTMLFormField
{
1543 public function __construct( $params ){
1544 parent
::__construct( $params );
1545 if( isset( $params['flatlist'] ) ){
1546 $this->mClass
.= ' mw-htmlform-multiselect-flatlist';
1550 function validate( $value, $alldata ) {
1551 $p = parent
::validate( $value, $alldata );
1553 if ( $p !== true ) {
1557 if ( !is_array( $value ) ) {
1561 # If all options are valid, array_intersect of the valid options
1562 # and the provided options will return the provided options.
1563 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
1565 $validValues = array_intersect( $value, $validOptions );
1566 if ( count( $validValues ) == count( $value ) ) {
1569 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1573 function getInputHTML( $value ) {
1574 $html = $this->formatOptions( $this->mParams
['options'], $value );
1579 function formatOptions( $options, $value ) {
1584 if ( !empty( $this->mParams
['disabled'] ) ) {
1585 $attribs['disabled'] = 'disabled';
1588 foreach ( $options as $label => $info ) {
1589 if ( is_array( $info ) ) {
1590 $html .= Html
::rawElement( 'h1', array(), $label ) . "\n";
1591 $html .= $this->formatOptions( $info, $value );
1593 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
1595 $checkbox = Xml
::check(
1596 $this->mName
. '[]',
1597 in_array( $info, $value, true ),
1598 $attribs +
$thisAttribs );
1599 $checkbox .= ' ' . Html
::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
1601 $html .= ' ' . Html
::rawElement( 'div', array( 'class' => 'mw-htmlform-multiselect-item' ), $checkbox );
1609 * @param $request WebRequest
1612 function loadDataFromRequest( $request ) {
1613 if ( $this->mParent
->getMethod() == 'post' ) {
1614 if( $request->wasPosted() ){
1615 # Checkboxes are just not added to the request arrays if they're not checked,
1616 # so it's perfectly possible for there not to be an entry at all
1617 return $request->getArray( $this->mName
, array() );
1619 # That's ok, the user has not yet submitted the form, so show the defaults
1620 return $this->getDefault();
1623 # This is the impossible case: if we look at $_GET and see no data for our
1624 # field, is it because the user has not yet submitted the form, or that they
1625 # have submitted it with all the options unchecked? We will have to assume the
1626 # latter, which basically means that you can't specify 'positive' defaults
1629 return $request->getArray( $this->mName
, array() );
1633 function getDefault() {
1634 if ( isset( $this->mDefault
) ) {
1635 return $this->mDefault
;
1641 protected function needsLabel() {
1647 * Double field with a dropdown list constructed from a system message in the format
1650 * * New Optgroup header
1651 * Plus a text field underneath for an additional reason. The 'value' of the field is
1652 * ""<select>: <extra reason>"", or "<extra reason>" if nothing has been selected in the
1654 * @todo FIXME: If made 'required', only the text field should be compulsory.
1656 class HTMLSelectAndOtherField
extends HTMLSelectField
{
1658 function __construct( $params ) {
1659 if ( array_key_exists( 'other', $params ) ) {
1660 } elseif( array_key_exists( 'other-message', $params ) ){
1661 $params['other'] = wfMessage( $params['other-message'] )->plain();
1663 $params['other'] = null;
1666 if ( array_key_exists( 'options', $params ) ) {
1667 # Options array already specified
1668 } elseif( array_key_exists( 'options-message', $params ) ){
1669 # Generate options array from a system message
1670 $params['options'] = self
::parseMessage(
1671 wfMessage( $params['options-message'] )->inContentLanguage()->plain(),
1676 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
1678 $this->mFlatOptions
= self
::flattenOptions( $params['options'] );
1680 parent
::__construct( $params );
1684 * Build a drop-down box from a textual list.
1685 * @param $string String message text
1686 * @param $otherName String name of "other reason" option
1688 * TODO: this is copied from Xml::listDropDown(), deprecate/avoid duplication?
1690 public static function parseMessage( $string, $otherName=null ) {
1691 if( $otherName === null ){
1692 $otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
1696 $options = array( $otherName => 'other' );
1698 foreach ( explode( "\n", $string ) as $option ) {
1699 $value = trim( $option );
1700 if ( $value == '' ) {
1702 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
1703 # A new group is starting...
1704 $value = trim( substr( $value, 1 ) );
1706 } elseif ( substr( $value, 0, 2) == '**' ) {
1708 $opt = trim( substr( $value, 2 ) );
1709 if( $optgroup === false ){
1710 $options[$opt] = $opt;
1712 $options[$optgroup][$opt] = $opt;
1715 # groupless reason list
1717 $options[$option] = $option;
1724 function getInputHTML( $value ) {
1725 $select = parent
::getInputHTML( $value[1] );
1727 $textAttribs = array(
1728 'id' => $this->mID
. '-other',
1729 'size' => $this->getSize(),
1732 foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
1733 if ( isset( $this->mParams
[$param] ) ) {
1734 $textAttribs[$param] = '';
1738 $textbox = Html
::input(
1739 $this->mName
. '-other',
1745 return "$select<br />\n$textbox";
1749 * @param $request WebRequest
1750 * @return Array( <overall message>, <select value>, <text field value> )
1752 function loadDataFromRequest( $request ) {
1753 if ( $request->getCheck( $this->mName
) ) {
1755 $list = $request->getText( $this->mName
);
1756 $text = $request->getText( $this->mName
. '-other' );
1758 if ( $list == 'other' ) {
1760 } elseif( !in_array( $list, $this->mFlatOptions
) ){
1761 # User has spoofed the select form to give an option which wasn't
1762 # in the original offer. Sulk...
1764 } elseif( $text == '' ) {
1767 $final = $list . wfMsgForContent( 'colon-separator' ) . $text;
1771 $final = $this->getDefault();
1775 foreach ( $this->mFlatOptions
as $option ) {
1776 $match = $option . wfMsgForContent( 'colon-separator' );
1777 if( strpos( $text, $match ) === 0 ) {
1779 $text = substr( $text, strlen( $match ) );
1784 return array( $final, $list, $text );
1787 function getSize() {
1788 return isset( $this->mParams
['size'] )
1789 ?
$this->mParams
['size']
1793 function validate( $value, $alldata ) {
1794 # HTMLSelectField forces $value to be one of the options in the select
1795 # field, which is not useful here. But we do want the validation further up
1797 $p = parent
::validate( $value[1], $alldata );
1799 if ( $p !== true ) {
1803 if( isset( $this->mParams
['required'] ) && $value[1] === '' ){
1804 return wfMsgExt( 'htmlform-required', 'parseinline' );
1812 * Radio checkbox fields.
1814 class HTMLRadioField
extends HTMLFormField
{
1815 function validate( $value, $alldata ) {
1816 $p = parent
::validate( $value, $alldata );
1818 if ( $p !== true ) {
1822 if ( !is_string( $value ) && !is_int( $value ) ) {
1826 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
1828 if ( in_array( $value, $validOptions ) ) {
1831 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1836 * This returns a block of all the radio options, in one cell.
1837 * @see includes/HTMLFormField#getInputHTML()
1838 * @param $value String
1841 function getInputHTML( $value ) {
1842 $html = $this->formatOptions( $this->mParams
['options'], $value );
1847 function formatOptions( $options, $value ) {
1851 if ( !empty( $this->mParams
['disabled'] ) ) {
1852 $attribs['disabled'] = 'disabled';
1855 # TODO: should this produce an unordered list perhaps?
1856 foreach ( $options as $label => $info ) {
1857 if ( is_array( $info ) ) {
1858 $html .= Html
::rawElement( 'h1', array(), $label ) . "\n";
1859 $html .= $this->formatOptions( $info, $value );
1861 $id = Sanitizer
::escapeId( $this->mID
. "-$info" );
1862 $html .= Xml
::radio(
1866 $attribs +
array( 'id' => $id )
1869 Html
::rawElement( 'label', array( 'for' => $id ), $label );
1871 $html .= "<br />\n";
1878 protected function needsLabel() {
1884 * An information field (text blob), not a proper input.
1886 class HTMLInfoField
extends HTMLFormField
{
1887 function __construct( $info ) {
1888 $info['nodata'] = true;
1890 parent
::__construct( $info );
1893 function getInputHTML( $value ) {
1894 return !empty( $this->mParams
['raw'] ) ?
$value : htmlspecialchars( $value );
1897 function getTableRow( $value ) {
1898 if ( !empty( $this->mParams
['rawrow'] ) ) {
1902 return parent
::getTableRow( $value );
1905 protected function needsLabel() {
1910 class HTMLHiddenField
extends HTMLFormField
{
1911 public function __construct( $params ) {
1912 parent
::__construct( $params );
1914 # Per HTML5 spec, hidden fields cannot be 'required'
1915 # http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state
1916 unset( $this->mParams
['required'] );
1919 public function getTableRow( $value ) {
1922 $params['id'] = $this->mID
;
1925 $this->mParent
->addHiddenField(
1934 public function getInputHTML( $value ) { return ''; }
1938 * Add a submit button inline in the form (as opposed to
1939 * HTMLForm::addButton(), which will add it at the end).
1941 class HTMLSubmitField
extends HTMLFormField
{
1943 function __construct( $info ) {
1944 $info['nodata'] = true;
1945 parent
::__construct( $info );
1948 function getInputHTML( $value ) {
1949 return Xml
::submitButton(
1952 'class' => 'mw-htmlform-submit',
1953 'name' => $this->mName
,
1959 protected function needsLabel() {
1964 * Button cannot be invalid
1965 * @param $value String
1966 * @param $alldata Array
1969 public function validate( $value, $alldata ){
1974 class HTMLEditTools
extends HTMLFormField
{
1975 public function getInputHTML( $value ) {
1979 public function getTableRow( $value ) {
1980 if ( empty( $this->mParams
['message'] ) ) {
1981 $msg = wfMessage( 'edittools' );
1983 $msg = wfMessage( $this->mParams
['message'] );
1984 if ( $msg->isDisabled() ) {
1985 $msg = wfMessage( 'edittools' );
1988 $msg->inContentLanguage();
1991 return '<tr><td></td><td class="mw-input">'
1992 . '<div class="mw-editTools">'
1993 . $msg->parseAsBlock()
1994 . "</div></td></tr>\n";