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 text for a message to use as a help text.
34 * 'help-message' -- message key for a message to use as a help text.
35 * can be an array of msg key and then parameters to
37 * Overwrites 'help-messages' and 'help'.
38 * 'help-messages' -- array of message key. As above, each item can
39 * be an array of msg key and then parameters.
41 * 'required' -- passed through to the object, indicating that it
42 * is a required field.
43 * 'size' -- the length of text fields
44 * 'filter-callback -- a function name to give you the chance to
45 * massage the inputted value before it's processed.
46 * @see HTMLForm::filter()
47 * 'validation-callback' -- a function name to give you the chance
48 * to impose extra validation on the field input.
49 * @see HTMLForm::validate()
50 * 'name' -- By default, the 'name' attribute of the input field
51 * is "wp{$fieldname}". If you want a different name
52 * (eg one without the "wp" prefix), specify it here and
53 * it will be used without modification.
55 * TODO: Document 'section' / 'subsection' stuff
57 class HTMLForm
extends ContextSource
{
59 // A mapping of 'type' inputs onto standard HTMLFormField subclasses
60 static $typeMappings = array(
61 'text' => 'HTMLTextField',
62 'textarea' => 'HTMLTextAreaField',
63 'select' => 'HTMLSelectField',
64 'radio' => 'HTMLRadioField',
65 'multiselect' => 'HTMLMultiSelectField',
66 'check' => 'HTMLCheckField',
67 'toggle' => 'HTMLCheckField',
68 'int' => 'HTMLIntField',
69 'float' => 'HTMLFloatField',
70 'info' => 'HTMLInfoField',
71 'selectorother' => 'HTMLSelectOrOtherField',
72 'selectandother' => 'HTMLSelectAndOtherField',
73 'submit' => 'HTMLSubmitField',
74 'hidden' => 'HTMLHiddenField',
75 'edittools' => 'HTMLEditTools',
77 // HTMLTextField will output the correct type="" attribute automagically.
78 // There are about four zillion other HTML5 input types, like url, but
79 // we don't use those at the moment, so no point in adding all of them.
80 'email' => 'HTMLTextField',
81 'password' => 'HTMLTextField',
84 protected $mMessagePrefix;
86 /** @var HTMLFormField[] */
87 protected $mFlatFields;
89 protected $mFieldTree;
90 protected $mShowReset = false;
93 protected $mSubmitCallback;
94 protected $mValidationErrorMessage;
97 protected $mHeader = '';
98 protected $mFooter = '';
99 protected $mSectionHeaders = array();
100 protected $mSectionFooters = array();
101 protected $mPost = '';
104 protected $mSubmitID;
105 protected $mSubmitName;
106 protected $mSubmitText;
107 protected $mSubmitTooltip;
110 protected $mMethod = 'post';
113 * Form action URL. false means we will use the URL to set Title
117 protected $mAction = false;
119 protected $mUseMultipart = false;
120 protected $mHiddenFields = array();
121 protected $mButtons = array();
123 protected $mWrapperLegend = false;
126 * If true, sections that contain both fields and subsections will
127 * render their subsections before their fields.
129 * Subclasses may set this to false to render subsections after fields
132 protected $mSubSectionBeforeFields = true;
135 * Build a new HTMLForm from an array of field attributes
136 * @param $descriptor Array of Field constructs, as described above
137 * @param $context IContextSource available since 1.18, will become compulsory in 1.18.
138 * Obviates the need to call $form->setTitle()
139 * @param $messagePrefix String a prefix to go in front of default messages
141 public function __construct( $descriptor, /*IContextSource*/ $context = null, $messagePrefix = '' ) {
142 if( $context instanceof IContextSource
){
143 $this->setContext( $context );
144 $this->mTitle
= false; // We don't need them to set a title
145 $this->mMessagePrefix
= $messagePrefix;
148 if( is_string( $context ) && $messagePrefix === '' ){
149 // it's actually $messagePrefix
150 $this->mMessagePrefix
= $context;
154 // Expand out into a tree.
155 $loadedDescriptor = array();
156 $this->mFlatFields
= array();
158 foreach ( $descriptor as $fieldname => $info ) {
159 $section = isset( $info['section'] )
163 if ( isset( $info['type'] ) && $info['type'] == 'file' ) {
164 $this->mUseMultipart
= true;
167 $field = self
::loadInputFromParameters( $fieldname, $info );
168 $field->mParent
= $this;
170 $setSection =& $loadedDescriptor;
172 $sectionParts = explode( '/', $section );
174 while ( count( $sectionParts ) ) {
175 $newName = array_shift( $sectionParts );
177 if ( !isset( $setSection[$newName] ) ) {
178 $setSection[$newName] = array();
181 $setSection =& $setSection[$newName];
185 $setSection[$fieldname] = $field;
186 $this->mFlatFields
[$fieldname] = $field;
189 $this->mFieldTree
= $loadedDescriptor;
193 * Add the HTMLForm-specific JavaScript, if it hasn't been
195 * @deprecated since 1.18 load modules with ResourceLoader instead
197 static function addJS() { wfDeprecated( __METHOD__
, '1.18' ); }
200 * Initialise a new Object for the field
201 * @param $fieldname string
202 * @param $descriptor string input Descriptor, as described above
203 * @return HTMLFormField subclass
205 static function loadInputFromParameters( $fieldname, $descriptor ) {
206 if ( isset( $descriptor['class'] ) ) {
207 $class = $descriptor['class'];
208 } elseif ( isset( $descriptor['type'] ) ) {
209 $class = self
::$typeMappings[$descriptor['type']];
210 $descriptor['class'] = $class;
216 throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
219 $descriptor['fieldname'] = $fieldname;
222 # This will throw a fatal error whenever someone try to use
223 # 'class' to feed a CSS class instead of 'cssclass'. Would be
224 # great to avoid the fatal error and show a nice error.
225 $obj = new $class( $descriptor );
231 * Prepare form for submission
233 function prepareForm() {
234 # Check if we have the info we need
235 if ( !$this->mTitle
instanceof Title
&& $this->mTitle
!== false ) {
236 throw new MWException( "You must call setTitle() on an HTMLForm" );
239 # Load data from the request.
244 * Try submitting, with edit token check first
245 * @return Status|boolean
247 function tryAuthorizedSubmit() {
251 if ( $this->getMethod() != 'post' ) {
252 $submit = true; // no session check needed
253 } elseif ( $this->getRequest()->wasPosted() ) {
254 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
255 if ( $this->getUser()->isLoggedIn() ||
$editToken != null ) {
256 // Session tokens for logged-out users have no security value.
257 // However, if the user gave one, check it in order to give a nice
258 // "session expired" error instead of "permission denied" or such.
259 $submit = $this->getUser()->matchEditToken( $editToken );
266 $result = $this->trySubmit();
273 * The here's-one-I-made-earlier option: do the submission if
274 * posted, or display the form with or without funky validation
276 * @return Bool or Status whether submission was successful.
279 $this->prepareForm();
281 $result = $this->tryAuthorizedSubmit();
282 if ( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
286 $this->displayForm( $result );
291 * Validate all the fields, and call the submision callback
292 * function if everything is kosher.
293 * @return Mixed Bool true == Successful submission, Bool false
294 * == No submission attempted, anything else == Error to
297 function trySubmit() {
298 # Check for validation
299 foreach ( $this->mFlatFields
as $fieldname => $field ) {
300 if ( !empty( $field->mParams
['nodata'] ) ) {
303 if ( $field->validate(
304 $this->mFieldData
[$fieldname],
308 return isset( $this->mValidationErrorMessage
)
309 ?
$this->mValidationErrorMessage
310 : array( 'htmlform-invalid-input' );
314 $callback = $this->mSubmitCallback
;
316 $data = $this->filterDataForSubmit( $this->mFieldData
);
318 $res = call_user_func( $callback, $data, $this );
324 * Set a callback to a function to do something with the form
325 * once it's been successfully validated.
326 * @param $cb String function name. The function will be passed
327 * the output from HTMLForm::filterDataForSubmit, and must
328 * return Bool true on success, Bool false if no submission
329 * was attempted, or String HTML output to display on error.
331 function setSubmitCallback( $cb ) {
332 $this->mSubmitCallback
= $cb;
336 * Set a message to display on a validation error.
337 * @param $msg Mixed String or Array of valid inputs to wfMsgExt()
338 * (so each entry can be either a String or Array)
340 function setValidationErrorMessage( $msg ) {
341 $this->mValidationErrorMessage
= $msg;
345 * Set the introductory message, overwriting any existing message.
346 * @param $msg String complete text of message to display
348 function setIntro( $msg ) {
349 $this->setPreText( $msg );
353 * Set the introductory message, overwriting any existing message.
355 * @param $msg String complete text of message to display
357 function setPreText( $msg ) { $this->mPre
= $msg; }
360 * Add introductory text.
361 * @param $msg String complete text of message to display
363 function addPreText( $msg ) { $this->mPre
.= $msg; }
366 * Add header text, inside the form.
367 * @param $msg String complete text of message to display
368 * @param $section string The section to add the header to
370 function addHeaderText( $msg, $section = null ) {
371 if ( is_null( $section ) ) {
372 $this->mHeader
.= $msg;
374 if ( !isset( $this->mSectionHeaders
[$section] ) ) {
375 $this->mSectionHeaders
[$section] = '';
377 $this->mSectionHeaders
[$section] .= $msg;
382 * Set header text, inside the form.
384 * @param $msg String complete text of message to display
385 * @param $section The section to add the header to
387 function setHeaderText( $msg, $section = null ) {
388 if ( is_null( $section ) ) {
389 $this->mHeader
= $msg;
391 $this->mSectionHeaders
[$section] = $msg;
396 * Add footer text, inside the form.
397 * @param $msg String complete text of message to display
398 * @param $section string The section to add the footer text to
400 function addFooterText( $msg, $section = null ) {
401 if ( is_null( $section ) ) {
402 $this->mFooter
.= $msg;
404 if ( !isset( $this->mSectionFooters
[$section] ) ) {
405 $this->mSectionFooters
[$section] = '';
407 $this->mSectionFooters
[$section] .= $msg;
412 * Set footer text, inside the form.
414 * @param $msg String complete text of message to display
415 * @param $section string The section to add the footer text to
417 function setFooterText( $msg, $section = null ) {
418 if ( is_null( $section ) ) {
419 $this->mFooter
= $msg;
421 $this->mSectionFooters
[$section] = $msg;
426 * Add text to the end of the display.
427 * @param $msg String complete text of message to display
429 function addPostText( $msg ) { $this->mPost
.= $msg; }
432 * Set text at the end of the display.
433 * @param $msg String complete text of message to display
435 function setPostText( $msg ) { $this->mPost
= $msg; }
438 * Add a hidden field to the output
439 * @param $name String field name. This will be used exactly as entered
440 * @param $value String field value
441 * @param $attribs Array
443 public function addHiddenField( $name, $value, $attribs = array() ) {
444 $attribs +
= array( 'name' => $name );
445 $this->mHiddenFields
[] = array( $value, $attribs );
448 public function addButton( $name, $value, $id = null, $attribs = null ) {
449 $this->mButtons
[] = compact( 'name', 'value', 'id', 'attribs' );
453 * Display the form (sending to $wgOut), with an appropriate error
454 * message or stack of messages, and any validation errors, etc.
455 * @param $submitResult Mixed output from HTMLForm::trySubmit()
457 function displayForm( $submitResult ) {
458 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
462 * Returns the raw HTML generated by the form
463 * @param $submitResult Mixed output from HTMLForm::trySubmit()
466 function getHTML( $submitResult ) {
467 # For good measure (it is the default)
468 $this->getOutput()->preventClickjacking();
469 $this->getOutput()->addModules( 'mediawiki.htmlform' );
472 . $this->getErrors( $submitResult )
475 . $this->getHiddenFields()
476 . $this->getButtons()
480 $html = $this->wrapForm( $html );
482 return '' . $this->mPre
. $html . $this->mPost
;
486 * Wrap the form innards in an actual <form> element
487 * @param $html String HTML contents to wrap.
488 * @return String wrapped HTML.
490 function wrapForm( $html ) {
492 # Include a <fieldset> wrapper for style, if requested.
493 if ( $this->mWrapperLegend
!== false ) {
494 $html = Xml
::fieldset( $this->mWrapperLegend
, $html );
496 # Use multipart/form-data
497 $encType = $this->mUseMultipart
498 ?
'multipart/form-data'
499 : 'application/x-www-form-urlencoded';
502 'action' => $this->mAction
=== false ?
$this->getTitle()->getFullURL() : $this->mAction
,
503 'method' => $this->mMethod
,
504 'class' => 'visualClear',
505 'enctype' => $encType,
507 if ( !empty( $this->mId
) ) {
508 $attribs['id'] = $this->mId
;
511 return Html
::rawElement( 'form', $attribs, $html );
515 * Get the hidden fields that should go inside the form.
516 * @return String HTML.
518 function getHiddenFields() {
519 global $wgUsePathInfo;
522 if( $this->getMethod() == 'post' ){
523 $html .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
524 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
527 if ( !$wgUsePathInfo && $this->getMethod() == 'get' ) {
528 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
531 foreach ( $this->mHiddenFields
as $data ) {
532 list( $value, $attribs ) = $data;
533 $html .= Html
::hidden( $attribs['name'], $value, $attribs ) . "\n";
540 * Get the submit and (potentially) reset buttons.
541 * @return String HTML.
543 function getButtons() {
547 if ( isset( $this->mSubmitID
) ) {
548 $attribs['id'] = $this->mSubmitID
;
551 if ( isset( $this->mSubmitName
) ) {
552 $attribs['name'] = $this->mSubmitName
;
555 if ( isset( $this->mSubmitTooltip
) ) {
556 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
559 $attribs['class'] = 'mw-htmlform-submit';
561 $html .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
563 if ( $this->mShowReset
) {
564 $html .= Html
::element(
568 'value' => wfMsg( 'htmlform-reset' )
573 foreach ( $this->mButtons
as $button ) {
576 'name' => $button['name'],
577 'value' => $button['value']
580 if ( $button['attribs'] ) {
581 $attrs +
= $button['attribs'];
584 if ( isset( $button['id'] ) ) {
585 $attrs['id'] = $button['id'];
588 $html .= Html
::element( 'input', $attrs );
595 * Get the whole body of the form.
599 return $this->displaySection( $this->mFieldTree
);
603 * Format and display an error message stack.
604 * @param $errors String|Array|Status
607 function getErrors( $errors ) {
608 if ( $errors instanceof Status
) {
609 if ( $errors->isOK() ) {
612 $errorstr = $this->getOutput()->parse( $errors->getWikiText() );
614 } elseif ( is_array( $errors ) ) {
615 $errorstr = $this->formatErrors( $errors );
621 ? Html
::rawElement( 'div', array( 'class' => 'error' ), $errorstr )
626 * Format a stack of error messages into a single HTML string
627 * @param $errors Array of message keys/values
628 * @return String HTML, a <ul> list of errors
630 public static function formatErrors( $errors ) {
633 foreach ( $errors as $error ) {
634 if ( is_array( $error ) ) {
635 $msg = array_shift( $error );
641 $errorstr .= Html
::rawElement(
644 wfMsgExt( $msg, array( 'parseinline' ), $error )
648 $errorstr = Html
::rawElement( 'ul', array(), $errorstr );
654 * Set the text for the submit button
655 * @param $t String plaintext.
657 function setSubmitText( $t ) {
658 $this->mSubmitText
= $t;
662 * Set the text for the submit button to a message
664 * @param $msg String message key
666 public function setSubmitTextMsg( $msg ) {
667 return $this->setSubmitText( $this->msg( $msg )->escaped() );
671 * Get the text for the submit button, either customised or a default.
674 function getSubmitText() {
675 return $this->mSubmitText
677 : wfMsg( 'htmlform-submit' );
680 public function setSubmitName( $name ) {
681 $this->mSubmitName
= $name;
684 public function setSubmitTooltip( $name ) {
685 $this->mSubmitTooltip
= $name;
689 * Set the id for the submit button.
691 * @todo FIXME: Integrity of $t is *not* validated
693 function setSubmitID( $t ) {
694 $this->mSubmitID
= $t;
697 public function setId( $id ) {
701 * Prompt the whole form to be wrapped in a <fieldset>, with
702 * this text as its <legend> element.
703 * @param $legend String HTML to go inside the <legend> element.
706 public function setWrapperLegend( $legend ) { $this->mWrapperLegend
= $legend; }
709 * Prompt the whole form to be wrapped in a <fieldset>, with
710 * this message as its <legend> element.
712 * @param $msg String message key
714 public function setWrapperLegendMsg( $msg ) {
715 return $this->setWrapperLegend( $this->msg( $msg )->escaped() );
719 * Set the prefix for various default messages
720 * TODO: currently only used for the <fieldset> legend on forms
721 * with multiple sections; should be used elsewhre?
724 function setMessagePrefix( $p ) {
725 $this->mMessagePrefix
= $p;
729 * Set the title for form submission
730 * @param $t Title of page the form is on/should be posted to
732 function setTitle( $t ) {
740 function getTitle() {
741 return $this->mTitle
=== false
742 ?
$this->getContext()->getTitle()
747 * Set the method used to submit the form
748 * @param $method String
750 public function setMethod( $method = 'post' ) {
751 $this->mMethod
= $method;
754 public function getMethod() {
755 return $this->mMethod
;
760 * @param $fields array[]|HTMLFormField[] array of fields (either arrays or objects)
761 * @param $sectionName string ID attribute of the <table> tag for this section, ignored if empty
762 * @param $fieldsetIDPrefix string ID prefix for the <fieldset> tag of each subsection, ignored if empty
765 function displaySection( $fields, $sectionName = '', $fieldsetIDPrefix = '' ) {
767 $subsectionHtml = '';
768 $hasLeftColumn = false;
770 foreach ( $fields as $key => $value ) {
771 if ( is_object( $value ) ) {
772 $v = empty( $value->mParams
['nodata'] )
773 ?
$this->mFieldData
[$key]
774 : $value->getDefault();
775 $tableHtml .= $value->getTableRow( $v );
777 if ( $value->getLabel() != ' ' ) {
778 $hasLeftColumn = true;
780 } elseif ( is_array( $value ) ) {
781 $section = $this->displaySection( $value, $key );
782 $legend = $this->getLegend( $key );
783 if ( isset( $this->mSectionHeaders
[$key] ) ) {
784 $section = $this->mSectionHeaders
[$key] . $section;
786 if ( isset( $this->mSectionFooters
[$key] ) ) {
787 $section .= $this->mSectionFooters
[$key];
789 $attributes = array();
790 if ( $fieldsetIDPrefix ) {
791 $attributes['id'] = Sanitizer
::escapeId( "$fieldsetIDPrefix$key" );
793 $subsectionHtml .= Xml
::fieldset( $legend, $section, $attributes ) . "\n";
799 if ( !$hasLeftColumn ) { // Avoid strange spacing when no labels exist
800 $classes[] = 'mw-htmlform-nolabel';
804 'class' => implode( ' ', $classes ),
807 if ( $sectionName ) {
808 $attribs['id'] = Sanitizer
::escapeId( "mw-htmlform-$sectionName" );
811 $tableHtml = Html
::rawElement( 'table', $attribs,
812 Html
::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n";
814 if ( $this->mSubSectionBeforeFields
) {
815 return $subsectionHtml . "\n" . $tableHtml;
817 return $tableHtml . "\n" . $subsectionHtml;
822 * Construct the form fields from the Descriptor array
824 function loadData() {
825 $fieldData = array();
827 foreach ( $this->mFlatFields
as $fieldname => $field ) {
828 if ( !empty( $field->mParams
['nodata'] ) ) {
830 } elseif ( !empty( $field->mParams
['disabled'] ) ) {
831 $fieldData[$fieldname] = $field->getDefault();
833 $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() );
838 foreach ( $fieldData as $name => &$value ) {
839 $field = $this->mFlatFields
[$name];
840 $value = $field->filter( $value, $this->mFlatFields
);
843 $this->mFieldData
= $fieldData;
847 * Stop a reset button being shown for this form
848 * @param $suppressReset Bool set to false to re-enable the
851 function suppressReset( $suppressReset = true ) {
852 $this->mShowReset
= !$suppressReset;
856 * Overload this if you want to apply special filtration routines
857 * to the form as a whole, after it's submitted but before it's
862 function filterDataForSubmit( $data ) {
867 * Get a string to go in the <legend> of a section fieldset. Override this if you
868 * want something more complicated
872 public function getLegend( $key ) {
873 return wfMsg( "{$this->mMessagePrefix}-$key" );
877 * Set the value for the action attribute of the form.
878 * When set to false (which is the default state), the set title is used.
882 * @param string|bool $action
884 public function setAction( $action ) {
885 $this->mAction
= $action;
891 * The parent class to generate form fields. Any field type should
892 * be a subclass of this.
894 abstract class HTMLFormField
{
896 protected $mValidationCallback;
897 protected $mFilterCallback;
900 protected $mLabel; # String label. Set on construction
902 protected $mClass = '';
911 * This function must be implemented to return the HTML to generate
912 * the input object itself. It should not implement the surrounding
913 * table cells/rows, or labels/help messages.
914 * @param $value String the value to set the input to; eg a default
915 * text for a text input.
916 * @return String valid HTML.
918 abstract function getInputHTML( $value );
921 * Override this function to add specific validation checks on the
922 * field input. Don't forget to call parent::validate() to ensure
923 * that the user-defined callback mValidationCallback is still run
924 * @param $value String the value the field was submitted with
925 * @param $alldata Array the data collected from the form
926 * @return Mixed Bool true on success, or String error to display.
928 function validate( $value, $alldata ) {
929 if ( isset( $this->mParams
['required'] ) && $value === '' ) {
930 return wfMsgExt( 'htmlform-required', 'parseinline' );
933 if ( isset( $this->mValidationCallback
) ) {
934 return call_user_func( $this->mValidationCallback
, $value, $alldata, $this->mParent
);
940 function filter( $value, $alldata ) {
941 if ( isset( $this->mFilterCallback
) ) {
942 $value = call_user_func( $this->mFilterCallback
, $value, $alldata, $this->mParent
);
949 * Should this field have a label, or is there no input element with the
950 * appropriate id for the label to point to?
952 * @return bool True to output a label, false to suppress
954 protected function needsLabel() {
959 * Get the value that this input has been set to from a posted form,
960 * or the input's default value if it has not been set.
961 * @param $request WebRequest
962 * @return String the value
964 function loadDataFromRequest( $request ) {
965 if ( $request->getCheck( $this->mName
) ) {
966 return $request->getText( $this->mName
);
968 return $this->getDefault();
973 * Initialise the object
974 * @param $params array Associative Array. See HTMLForm doc for syntax.
976 function __construct( $params ) {
977 $this->mParams
= $params;
979 # Generate the label from a message, if possible
980 if ( isset( $params['label-message'] ) ) {
981 $msgInfo = $params['label-message'];
983 if ( is_array( $msgInfo ) ) {
984 $msg = array_shift( $msgInfo );
990 $this->mLabel
= wfMsgExt( $msg, 'parseinline', $msgInfo );
991 } elseif ( isset( $params['label'] ) ) {
992 $this->mLabel
= $params['label'];
995 $this->mName
= "wp{$params['fieldname']}";
996 if ( isset( $params['name'] ) ) {
997 $this->mName
= $params['name'];
1000 $validName = Sanitizer
::escapeId( $this->mName
);
1001 if ( $this->mName
!= $validName && !isset( $params['nodata'] ) ) {
1002 throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__
);
1005 $this->mID
= "mw-input-{$this->mName}";
1007 if ( isset( $params['default'] ) ) {
1008 $this->mDefault
= $params['default'];
1011 if ( isset( $params['id'] ) ) {
1012 $id = $params['id'];
1013 $validId = Sanitizer
::escapeId( $id );
1015 if ( $id != $validId ) {
1016 throw new MWException( "Invalid id '$id' passed to " . __METHOD__
);
1022 if ( isset( $params['cssclass'] ) ) {
1023 $this->mClass
= $params['cssclass'];
1026 if ( isset( $params['validation-callback'] ) ) {
1027 $this->mValidationCallback
= $params['validation-callback'];
1030 if ( isset( $params['filter-callback'] ) ) {
1031 $this->mFilterCallback
= $params['filter-callback'];
1034 if ( isset( $params['flatlist'] ) ){
1035 $this->mClass
.= ' mw-htmlform-flatlist';
1040 * Get the complete table row for the input, including help text,
1041 * labels, and whatever.
1042 * @param $value String the value to set the input to.
1043 * @return String complete HTML table row.
1045 function getTableRow( $value ) {
1046 # Check for invalid data.
1048 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
1050 $cellAttributes = array();
1051 $verticalLabel = false;
1053 if ( !empty($this->mParams
['vertical-label']) ) {
1054 $cellAttributes['colspan'] = 2;
1055 $verticalLabel = true;
1058 if ( $errors === true ||
( !$this->mParent
->getRequest()->wasPosted() && ( $this->mParent
->getMethod() == 'post' ) ) ) {
1062 $errors = self
::formatErrors( $errors );
1063 $errorClass = 'mw-htmlform-invalid-input';
1066 $label = $this->getLabelHtml( $cellAttributes );
1067 $field = Html
::rawElement(
1069 array( 'class' => 'mw-input' ) +
$cellAttributes,
1070 $this->getInputHTML( $value ) . "\n$errors"
1073 $fieldType = get_class( $this );
1075 if ( $verticalLabel ) {
1076 $html = Html
::rawElement( 'tr',
1077 array( 'class' => 'mw-htmlform-vertical-label' ), $label );
1078 $html .= Html
::rawElement( 'tr',
1079 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
1082 $html = Html
::rawElement( 'tr',
1083 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
1089 if ( isset( $this->mParams
['help-message'] ) ) {
1090 $this->mParams
['help-messages'] = array( $this->mParams
['help-message'] );
1093 if ( isset( $this->mParams
['help-messages'] ) ) {
1094 foreach( $this->mParams
['help-messages'] as $name ) {
1095 $helpMessage = (array)$name;
1096 $msg = wfMessage( array_shift( $helpMessage ), $helpMessage );
1098 if( $msg->exists() ) {
1099 $helptext .= $msg->parse(); // Append message
1103 elseif ( isset( $this->mParams
['help'] ) ) {
1104 $helptext = $this->mParams
['help'];
1107 if ( !is_null( $helptext ) ) {
1108 $row = Html
::rawElement(
1110 array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
1113 $row = Html
::rawElement( 'tr', array(), $row );
1120 function getLabel() {
1121 return $this->mLabel
;
1123 function getLabelHtml( $cellAttributes = array() ) {
1124 # Don't output a for= attribute for labels with no associated input.
1125 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1128 if ( $this->needsLabel() ) {
1129 $for['for'] = $this->mID
;
1132 return Html
::rawElement( 'td', array( 'class' => 'mw-label' ) +
$cellAttributes,
1133 Html
::rawElement( 'label', $for, $this->getLabel() )
1137 function getDefault() {
1138 if ( isset( $this->mDefault
) ) {
1139 return $this->mDefault
;
1146 * Returns the attributes required for the tooltip and accesskey.
1148 * @return array Attributes
1150 public function getTooltipAndAccessKey() {
1151 if ( empty( $this->mParams
['tooltip'] ) ) {
1154 return Linker
::tooltipAndAccesskeyAttribs( $this->mParams
['tooltip'] );
1158 * flatten an array of options to a single array, for instance,
1159 * a set of <options> inside <optgroups>.
1160 * @param $options array Associative Array with values either Strings
1162 * @return Array flattened input
1164 public static function flattenOptions( $options ) {
1165 $flatOpts = array();
1167 foreach ( $options as $value ) {
1168 if ( is_array( $value ) ) {
1169 $flatOpts = array_merge( $flatOpts, self
::flattenOptions( $value ) );
1171 $flatOpts[] = $value;
1179 * Formats one or more errors as accepted by field validation-callback.
1180 * @param $errors String|Message|Array of strings or Message instances
1181 * @return String html
1184 protected static function formatErrors( $errors ) {
1185 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1186 $errors = array_shift( $errors );
1189 if ( is_array( $errors ) ) {
1191 foreach ( $errors as $error ) {
1192 if ( $error instanceof Message
) {
1193 $lines[] = Html
::rawElement( 'li', array(), $error->parse() );
1195 $lines[] = Html
::rawElement( 'li', array(), $error );
1198 return Html
::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
1200 if ( $errors instanceof Message
) {
1201 $errors = $errors->parse();
1203 return Html
::rawElement( 'span', array( 'class' => 'error' ), $errors );
1208 class HTMLTextField
extends HTMLFormField
{
1209 function getSize() {
1210 return isset( $this->mParams
['size'] )
1211 ?
$this->mParams
['size']
1215 function getInputHTML( $value ) {
1218 'name' => $this->mName
,
1219 'size' => $this->getSize(),
1221 ) +
$this->getTooltipAndAccessKey();
1223 if ( $this->mClass
!== '' ) {
1224 $attribs['class'] = $this->mClass
;
1227 if ( isset( $this->mParams
['maxlength'] ) ) {
1228 $attribs['maxlength'] = $this->mParams
['maxlength'];
1231 if ( !empty( $this->mParams
['disabled'] ) ) {
1232 $attribs['disabled'] = 'disabled';
1235 # TODO: Enforce pattern, step, required, readonly on the server side as
1237 foreach ( array( 'min', 'max', 'pattern', 'title', 'step',
1238 'placeholder' ) as $param ) {
1239 if ( isset( $this->mParams
[$param] ) ) {
1240 $attribs[$param] = $this->mParams
[$param];
1244 foreach ( array( 'required', 'autofocus', 'multiple', 'readonly' ) as $param ) {
1245 if ( isset( $this->mParams
[$param] ) ) {
1246 $attribs[$param] = '';
1250 # Implement tiny differences between some field variants
1251 # here, rather than creating a new class for each one which
1252 # is essentially just a clone of this one.
1253 if ( isset( $this->mParams
['type'] ) ) {
1254 switch ( $this->mParams
['type'] ) {
1256 $attribs['type'] = 'email';
1259 $attribs['type'] = 'number';
1262 $attribs['type'] = 'number';
1263 $attribs['step'] = 'any';
1268 $attribs['type'] = $this->mParams
['type'];
1273 return Html
::element( 'input', $attribs );
1276 class HTMLTextAreaField
extends HTMLFormField
{
1277 function getCols() {
1278 return isset( $this->mParams
['cols'] )
1279 ?
$this->mParams
['cols']
1283 function getRows() {
1284 return isset( $this->mParams
['rows'] )
1285 ?
$this->mParams
['rows']
1289 function getInputHTML( $value ) {
1292 'name' => $this->mName
,
1293 'cols' => $this->getCols(),
1294 'rows' => $this->getRows(),
1295 ) +
$this->getTooltipAndAccessKey();
1297 if ( $this->mClass
!== '' ) {
1298 $attribs['class'] = $this->mClass
;
1301 if ( !empty( $this->mParams
['disabled'] ) ) {
1302 $attribs['disabled'] = 'disabled';
1305 if ( !empty( $this->mParams
['readonly'] ) ) {
1306 $attribs['readonly'] = 'readonly';
1309 foreach ( array( 'required', 'autofocus' ) as $param ) {
1310 if ( isset( $this->mParams
[$param] ) ) {
1311 $attribs[$param] = '';
1315 return Html
::element( 'textarea', $attribs, $value );
1320 * A field that will contain a numeric value
1322 class HTMLFloatField
extends HTMLTextField
{
1323 function getSize() {
1324 return isset( $this->mParams
['size'] )
1325 ?
$this->mParams
['size']
1329 function validate( $value, $alldata ) {
1330 $p = parent
::validate( $value, $alldata );
1332 if ( $p !== true ) {
1336 $value = trim( $value );
1338 # http://dev.w3.org/html5/spec/common-microsyntaxes.html#real-numbers
1339 # with the addition that a leading '+' sign is ok.
1340 if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
1341 return wfMsgExt( 'htmlform-float-invalid', 'parse' );
1344 # The "int" part of these message names is rather confusing.
1345 # They make equal sense for all numbers.
1346 if ( isset( $this->mParams
['min'] ) ) {
1347 $min = $this->mParams
['min'];
1349 if ( $min > $value ) {
1350 return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
1354 if ( isset( $this->mParams
['max'] ) ) {
1355 $max = $this->mParams
['max'];
1357 if ( $max < $value ) {
1358 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
1367 * A field that must contain a number
1369 class HTMLIntField
extends HTMLFloatField
{
1370 function validate( $value, $alldata ) {
1371 $p = parent
::validate( $value, $alldata );
1373 if ( $p !== true ) {
1377 # http://dev.w3.org/html5/spec/common-microsyntaxes.html#signed-integers
1378 # with the addition that a leading '+' sign is ok. Note that leading zeros
1379 # are fine, and will be left in the input, which is useful for things like
1380 # phone numbers when you know that they are integers (the HTML5 type=tel
1381 # input does not require its value to be numeric). If you want a tidier
1382 # value to, eg, save in the DB, clean it up with intval().
1383 if ( !preg_match( '/^((\+|\-)?\d+)?$/', trim( $value ) )
1385 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
1395 class HTMLCheckField
extends HTMLFormField
{
1396 function getInputHTML( $value ) {
1397 if ( !empty( $this->mParams
['invert'] ) ) {
1401 $attr = $this->getTooltipAndAccessKey();
1402 $attr['id'] = $this->mID
;
1404 if ( !empty( $this->mParams
['disabled'] ) ) {
1405 $attr['disabled'] = 'disabled';
1408 if ( $this->mClass
!== '' ) {
1409 $attr['class'] = $this->mClass
;
1412 return Xml
::check( $this->mName
, $value, $attr ) . ' ' .
1413 Html
::rawElement( 'label', array( 'for' => $this->mID
), $this->mLabel
);
1417 * For a checkbox, the label goes on the right hand side, and is
1418 * added in getInputHTML(), rather than HTMLFormField::getRow()
1421 function getLabel() {
1426 * @param $request WebRequest
1429 function loadDataFromRequest( $request ) {
1431 if ( isset( $this->mParams
['invert'] ) && $this->mParams
['invert'] ) {
1435 // GetCheck won't work like we want for checks.
1436 // Fetch the value in either one of the two following case:
1437 // - we have a valid token (form got posted or GET forged by the user)
1438 // - checkbox name has a value (false or true), ie is not null
1439 if ( $request->getCheck( 'wpEditToken' ) ||
$request->getVal( $this->mName
)!== null ) {
1440 // XOR has the following truth table, which is what we want
1441 // INVERT VALUE | OUTPUT
1442 // true true | false
1443 // false true | true
1444 // false false | false
1445 // true false | true
1446 return $request->getBool( $this->mName
) xor $invert;
1448 return $this->getDefault();
1454 * A select dropdown field. Basically a wrapper for Xmlselect class
1456 class HTMLSelectField
extends HTMLFormField
{
1457 function validate( $value, $alldata ) {
1458 $p = parent
::validate( $value, $alldata );
1460 if ( $p !== true ) {
1464 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
1466 if ( in_array( $value, $validOptions ) )
1469 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1472 function getInputHTML( $value ) {
1473 $select = new XmlSelect( $this->mName
, $this->mID
, strval( $value ) );
1475 # If one of the options' 'name' is int(0), it is automatically selected.
1476 # because PHP sucks and thinks int(0) == 'some string'.
1477 # Working around this by forcing all of them to strings.
1478 foreach( $this->mParams
['options'] as &$opt ){
1479 if( is_int( $opt ) ){
1480 $opt = strval( $opt );
1483 unset( $opt ); # PHP keeps $opt around as a reference, which is a bit scary
1485 if ( !empty( $this->mParams
['disabled'] ) ) {
1486 $select->setAttribute( 'disabled', 'disabled' );
1489 if ( $this->mClass
!== '' ) {
1490 $select->setAttribute( 'class', $this->mClass
);
1493 $select->addOptions( $this->mParams
['options'] );
1495 return $select->getHTML();
1500 * Select dropdown field, with an additional "other" textbox.
1502 class HTMLSelectOrOtherField
extends HTMLTextField
{
1503 static $jsAdded = false;
1505 function __construct( $params ) {
1506 if ( !in_array( 'other', $params['options'], true ) ) {
1507 $msg = isset( $params['other'] ) ?
$params['other'] : wfMsg( 'htmlform-selectorother-other' );
1508 $params['options'][$msg] = 'other';
1511 parent
::__construct( $params );
1514 static function forceToStringRecursive( $array ) {
1515 if ( is_array( $array ) ) {
1516 return array_map( array( __CLASS__
, 'forceToStringRecursive' ), $array );
1518 return strval( $array );
1522 function getInputHTML( $value ) {
1523 $valInSelect = false;
1525 if ( $value !== false ) {
1526 $valInSelect = in_array(
1528 HTMLFormField
::flattenOptions( $this->mParams
['options'] )
1532 $selected = $valInSelect ?
$value : 'other';
1534 $opts = self
::forceToStringRecursive( $this->mParams
['options'] );
1536 $select = new XmlSelect( $this->mName
, $this->mID
, $selected );
1537 $select->addOptions( $opts );
1539 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
1541 $tbAttribs = array( 'id' => $this->mID
. '-other', 'size' => $this->getSize() );
1543 if ( !empty( $this->mParams
['disabled'] ) ) {
1544 $select->setAttribute( 'disabled', 'disabled' );
1545 $tbAttribs['disabled'] = 'disabled';
1548 $select = $select->getHTML();
1550 if ( isset( $this->mParams
['maxlength'] ) ) {
1551 $tbAttribs['maxlength'] = $this->mParams
['maxlength'];
1554 if ( $this->mClass
!== '' ) {
1555 $tbAttribs['class'] = $this->mClass
;
1558 $textbox = Html
::input(
1559 $this->mName
. '-other',
1560 $valInSelect ?
'' : $value,
1565 return "$select<br />\n$textbox";
1569 * @param $request WebRequest
1572 function loadDataFromRequest( $request ) {
1573 if ( $request->getCheck( $this->mName
) ) {
1574 $val = $request->getText( $this->mName
);
1576 if ( $val == 'other' ) {
1577 $val = $request->getText( $this->mName
. '-other' );
1582 return $this->getDefault();
1588 * Multi-select field
1590 class HTMLMultiSelectField
extends HTMLFormField
{
1592 function validate( $value, $alldata ) {
1593 $p = parent
::validate( $value, $alldata );
1595 if ( $p !== true ) {
1599 if ( !is_array( $value ) ) {
1603 # If all options are valid, array_intersect of the valid options
1604 # and the provided options will return the provided options.
1605 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
1607 $validValues = array_intersect( $value, $validOptions );
1608 if ( count( $validValues ) == count( $value ) ) {
1611 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1615 function getInputHTML( $value ) {
1616 $html = $this->formatOptions( $this->mParams
['options'], $value );
1621 function formatOptions( $options, $value ) {
1626 if ( !empty( $this->mParams
['disabled'] ) ) {
1627 $attribs['disabled'] = 'disabled';
1630 foreach ( $options as $label => $info ) {
1631 if ( is_array( $info ) ) {
1632 $html .= Html
::rawElement( 'h1', array(), $label ) . "\n";
1633 $html .= $this->formatOptions( $info, $value );
1635 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
1637 $checkbox = Xml
::check(
1638 $this->mName
. '[]',
1639 in_array( $info, $value, true ),
1640 $attribs +
$thisAttribs );
1641 $checkbox .= ' ' . Html
::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
1643 $html .= ' ' . Html
::rawElement( 'div', array( 'class' => 'mw-htmlform-flatlist-item' ), $checkbox );
1651 * @param $request WebRequest
1654 function loadDataFromRequest( $request ) {
1655 if ( $this->mParent
->getMethod() == 'post' ) {
1656 if( $request->wasPosted() ){
1657 # Checkboxes are just not added to the request arrays if they're not checked,
1658 # so it's perfectly possible for there not to be an entry at all
1659 return $request->getArray( $this->mName
, array() );
1661 # That's ok, the user has not yet submitted the form, so show the defaults
1662 return $this->getDefault();
1665 # This is the impossible case: if we look at $_GET and see no data for our
1666 # field, is it because the user has not yet submitted the form, or that they
1667 # have submitted it with all the options unchecked? We will have to assume the
1668 # latter, which basically means that you can't specify 'positive' defaults
1671 return $request->getArray( $this->mName
, array() );
1675 function getDefault() {
1676 if ( isset( $this->mDefault
) ) {
1677 return $this->mDefault
;
1683 protected function needsLabel() {
1689 * Double field with a dropdown list constructed from a system message in the format
1692 * * New Optgroup header
1693 * Plus a text field underneath for an additional reason. The 'value' of the field is
1694 * ""<select>: <extra reason>"", or "<extra reason>" if nothing has been selected in the
1696 * @todo FIXME: If made 'required', only the text field should be compulsory.
1698 class HTMLSelectAndOtherField
extends HTMLSelectField
{
1700 function __construct( $params ) {
1701 if ( array_key_exists( 'other', $params ) ) {
1702 } elseif( array_key_exists( 'other-message', $params ) ){
1703 $params['other'] = wfMessage( $params['other-message'] )->plain();
1705 $params['other'] = null;
1708 if ( array_key_exists( 'options', $params ) ) {
1709 # Options array already specified
1710 } elseif( array_key_exists( 'options-message', $params ) ){
1711 # Generate options array from a system message
1712 $params['options'] = self
::parseMessage(
1713 wfMessage( $params['options-message'] )->inContentLanguage()->plain(),
1718 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
1720 $this->mFlatOptions
= self
::flattenOptions( $params['options'] );
1722 parent
::__construct( $params );
1726 * Build a drop-down box from a textual list.
1727 * @param $string String message text
1728 * @param $otherName String name of "other reason" option
1730 * TODO: this is copied from Xml::listDropDown(), deprecate/avoid duplication?
1732 public static function parseMessage( $string, $otherName=null ) {
1733 if( $otherName === null ){
1734 $otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
1738 $options = array( $otherName => 'other' );
1740 foreach ( explode( "\n", $string ) as $option ) {
1741 $value = trim( $option );
1742 if ( $value == '' ) {
1744 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
1745 # A new group is starting...
1746 $value = trim( substr( $value, 1 ) );
1748 } elseif ( substr( $value, 0, 2) == '**' ) {
1750 $opt = trim( substr( $value, 2 ) );
1751 if( $optgroup === false ){
1752 $options[$opt] = $opt;
1754 $options[$optgroup][$opt] = $opt;
1757 # groupless reason list
1759 $options[$option] = $option;
1766 function getInputHTML( $value ) {
1767 $select = parent
::getInputHTML( $value[1] );
1769 $textAttribs = array(
1770 'id' => $this->mID
. '-other',
1771 'size' => $this->getSize(),
1774 if ( $this->mClass
!== '' ) {
1775 $textAttribs['class'] = $this->mClass
;
1778 foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
1779 if ( isset( $this->mParams
[$param] ) ) {
1780 $textAttribs[$param] = '';
1784 $textbox = Html
::input(
1785 $this->mName
. '-other',
1791 return "$select<br />\n$textbox";
1795 * @param $request WebRequest
1796 * @return Array( <overall message>, <select value>, <text field value> )
1798 function loadDataFromRequest( $request ) {
1799 if ( $request->getCheck( $this->mName
) ) {
1801 $list = $request->getText( $this->mName
);
1802 $text = $request->getText( $this->mName
. '-other' );
1804 if ( $list == 'other' ) {
1806 } elseif( !in_array( $list, $this->mFlatOptions
) ){
1807 # User has spoofed the select form to give an option which wasn't
1808 # in the original offer. Sulk...
1810 } elseif( $text == '' ) {
1813 $final = $list . wfMsgForContent( 'colon-separator' ) . $text;
1817 $final = $this->getDefault();
1821 foreach ( $this->mFlatOptions
as $option ) {
1822 $match = $option . wfMsgForContent( 'colon-separator' );
1823 if( strpos( $text, $match ) === 0 ) {
1825 $text = substr( $text, strlen( $match ) );
1830 return array( $final, $list, $text );
1833 function getSize() {
1834 return isset( $this->mParams
['size'] )
1835 ?
$this->mParams
['size']
1839 function validate( $value, $alldata ) {
1840 # HTMLSelectField forces $value to be one of the options in the select
1841 # field, which is not useful here. But we do want the validation further up
1843 $p = parent
::validate( $value[1], $alldata );
1845 if ( $p !== true ) {
1849 if( isset( $this->mParams
['required'] ) && $value[1] === '' ){
1850 return wfMsgExt( 'htmlform-required', 'parseinline' );
1858 * Radio checkbox fields.
1860 class HTMLRadioField
extends HTMLFormField
{
1863 function validate( $value, $alldata ) {
1864 $p = parent
::validate( $value, $alldata );
1866 if ( $p !== true ) {
1870 if ( !is_string( $value ) && !is_int( $value ) ) {
1874 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
1876 if ( in_array( $value, $validOptions ) ) {
1879 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1884 * This returns a block of all the radio options, in one cell.
1885 * @see includes/HTMLFormField#getInputHTML()
1886 * @param $value String
1889 function getInputHTML( $value ) {
1890 $html = $this->formatOptions( $this->mParams
['options'], $value );
1895 function formatOptions( $options, $value ) {
1899 if ( !empty( $this->mParams
['disabled'] ) ) {
1900 $attribs['disabled'] = 'disabled';
1903 # TODO: should this produce an unordered list perhaps?
1904 foreach ( $options as $label => $info ) {
1905 if ( is_array( $info ) ) {
1906 $html .= Html
::rawElement( 'h1', array(), $label ) . "\n";
1907 $html .= $this->formatOptions( $info, $value );
1909 $id = Sanitizer
::escapeId( $this->mID
. "-$info" );
1910 $radio = Xml
::radio(
1914 $attribs +
array( 'id' => $id )
1916 $radio .= ' ' .
1917 Html
::rawElement( 'label', array( 'for' => $id ), $label );
1919 $html .= ' ' . Html
::rawElement( 'div', array( 'class' => 'mw-htmlform-flatlist-item' ), $radio );
1926 protected function needsLabel() {
1932 * An information field (text blob), not a proper input.
1934 class HTMLInfoField
extends HTMLFormField
{
1935 function __construct( $info ) {
1936 $info['nodata'] = true;
1938 parent
::__construct( $info );
1941 function getInputHTML( $value ) {
1942 return !empty( $this->mParams
['raw'] ) ?
$value : htmlspecialchars( $value );
1945 function getTableRow( $value ) {
1946 if ( !empty( $this->mParams
['rawrow'] ) ) {
1950 return parent
::getTableRow( $value );
1953 protected function needsLabel() {
1958 class HTMLHiddenField
extends HTMLFormField
{
1959 public function __construct( $params ) {
1960 parent
::__construct( $params );
1962 # Per HTML5 spec, hidden fields cannot be 'required'
1963 # http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state
1964 unset( $this->mParams
['required'] );
1967 public function getTableRow( $value ) {
1970 $params['id'] = $this->mID
;
1973 $this->mParent
->addHiddenField(
1982 public function getInputHTML( $value ) { return ''; }
1986 * Add a submit button inline in the form (as opposed to
1987 * HTMLForm::addButton(), which will add it at the end).
1989 class HTMLSubmitField
extends HTMLFormField
{
1991 function __construct( $info ) {
1992 $info['nodata'] = true;
1993 parent
::__construct( $info );
1996 function getInputHTML( $value ) {
1997 return Xml
::submitButton(
2000 'class' => 'mw-htmlform-submit ' . $this->mClass
,
2001 'name' => $this->mName
,
2007 protected function needsLabel() {
2012 * Button cannot be invalid
2013 * @param $value String
2014 * @param $alldata Array
2017 public function validate( $value, $alldata ){
2022 class HTMLEditTools
extends HTMLFormField
{
2023 public function getInputHTML( $value ) {
2027 public function getTableRow( $value ) {
2028 if ( empty( $this->mParams
['message'] ) ) {
2029 $msg = wfMessage( 'edittools' );
2031 $msg = wfMessage( $this->mParams
['message'] );
2032 if ( $msg->isDisabled() ) {
2033 $msg = wfMessage( 'edittools' );
2036 $msg->inContentLanguage();
2039 return '<tr><td></td><td class="mw-input">'
2040 . '<div class="mw-editTools">'
2041 . $msg->parseAsBlock()
2042 . "</div></td></tr>\n";