3 * Object handling generic submission, CSRF protection, layout and
4 * other logic for UI forms. in a reusable manner.
6 * In order to generate the form, the HTMLForm object takes an array
7 * structure detailing the form fields available. Each element of the
8 * array is a basic property-list, including the type of field, the
9 * label it is to be given in the form, callbacks for validation and
10 * 'filtering', and other pertinent information.
12 * Field types are implemented as subclasses of the generic HTMLFormField
13 * object, and typically implement at least getInputHTML, which generates
14 * the HTML for the input field to be placed in the table.
16 * The constructor input is an associative array of $fieldname => $info,
17 * where $info is an Associative Array with any of the following:
19 * 'class' -- the subclass of HTMLFormField that will be used
20 * to create the object. *NOT* the CSS class!
21 * 'type' -- roughly translates into the <select> type attribute.
22 * if 'class' is not specified, this is used as a map
23 * through HTMLForm::$typeMappings to get the class name.
24 * 'default' -- default value when the form is displayed
25 * 'id' -- HTML id attribute
26 * 'cssclass' -- CSS class
27 * 'options' -- varies according to the specific object.
28 * 'label-message' -- message key for a message to use as the label.
29 * can be an array of msg key and then parameters to
31 * 'label' -- alternatively, a raw text message. Overridden by
33 * 'help-message' -- message key for a message to use as a help text.
34 * can be an array of msg key and then parameters to
36 * Overwrites 'help-messages'.
37 * 'help-messages' -- array of message key. As above, each item can
38 * be an array of msg key and then parameters.
39 * Overwrites 'help-message'.
40 * 'required' -- passed through to the object, indicating that it
41 * is a required field.
42 * 'size' -- the length of text fields
43 * 'filter-callback -- a function name to give you the chance to
44 * massage the inputted value before it's processed.
45 * @see HTMLForm::filter()
46 * 'validation-callback' -- a function name to give you the chance
47 * to impose extra validation on the field input.
48 * @see HTMLForm::validate()
49 * 'name' -- By default, the 'name' attribute of the input field
50 * is "wp{$fieldname}". If you want a different name
51 * (eg one without the "wp" prefix), specify it here and
52 * it will be used without modification.
54 * TODO: Document 'section' / 'subsection' stuff
56 class HTMLForm
extends ContextSource
{
58 # A mapping of 'type' inputs onto standard HTMLFormField subclasses
59 static $typeMappings = array(
60 'text' => 'HTMLTextField',
61 'textarea' => 'HTMLTextAreaField',
62 'select' => 'HTMLSelectField',
63 'radio' => 'HTMLRadioField',
64 'multiselect' => 'HTMLMultiSelectField',
65 'check' => 'HTMLCheckField',
66 'toggle' => 'HTMLCheckField',
67 'int' => 'HTMLIntField',
68 'float' => 'HTMLFloatField',
69 'info' => 'HTMLInfoField',
70 'selectorother' => 'HTMLSelectOrOtherField',
71 'selectandother' => 'HTMLSelectAndOtherField',
72 'submit' => 'HTMLSubmitField',
73 'hidden' => 'HTMLHiddenField',
74 'edittools' => 'HTMLEditTools',
76 # HTMLTextField will output the correct type="" attribute automagically.
77 # There are about four zillion other HTML5 input types, like url, but
78 # we don't use those at the moment, so no point in adding all of them.
79 'email' => 'HTMLTextField',
80 'password' => 'HTMLTextField',
83 protected $mMessagePrefix;
85 /** @var HTMLFormField[] */
86 protected $mFlatFields;
88 protected $mFieldTree;
89 protected $mShowReset = false;
92 protected $mSubmitCallback;
93 protected $mValidationErrorMessage;
96 protected $mHeader = '';
97 protected $mFooter = '';
98 protected $mSectionHeaders = array();
99 protected $mSectionFooters = array();
100 protected $mPost = '';
103 protected $mSubmitID;
104 protected $mSubmitName;
105 protected $mSubmitText;
106 protected $mSubmitTooltip;
109 protected $mMethod = 'post';
111 protected $mUseMultipart = false;
112 protected $mHiddenFields = array();
113 protected $mButtons = array();
115 protected $mWrapperLegend = false;
118 * If true, sections that contain both fields and subsections will
119 * render their subsections before their fields.
121 * Subclasses may set this to false to render subsections after fields
124 protected $mSubSectionBeforeFields = true;
127 * Build a new HTMLForm from an array of field attributes
128 * @param $descriptor Array of Field constructs, as described above
129 * @param $context IContextSource available since 1.18, will become compulsory in 1.18.
130 * Obviates the need to call $form->setTitle()
131 * @param $messagePrefix String a prefix to go in front of default messages
133 public function __construct( $descriptor, /*IContextSource*/ $context = null, $messagePrefix = '' ) {
134 if( $context instanceof IContextSource
){
135 $this->setContext( $context );
136 $this->mTitle
= false; // We don't need them to set a title
137 $this->mMessagePrefix
= $messagePrefix;
140 if( is_string( $context ) && $messagePrefix === '' ){
141 // it's actually $messagePrefix
142 $this->mMessagePrefix
= $context;
146 // Expand out into a tree.
147 $loadedDescriptor = array();
148 $this->mFlatFields
= array();
150 foreach ( $descriptor as $fieldname => $info ) {
151 $section = isset( $info['section'] )
155 if ( isset( $info['type'] ) && $info['type'] == 'file' ) {
156 $this->mUseMultipart
= true;
159 $field = self
::loadInputFromParameters( $fieldname, $info );
160 $field->mParent
= $this;
162 $setSection =& $loadedDescriptor;
164 $sectionParts = explode( '/', $section );
166 while ( count( $sectionParts ) ) {
167 $newName = array_shift( $sectionParts );
169 if ( !isset( $setSection[$newName] ) ) {
170 $setSection[$newName] = array();
173 $setSection =& $setSection[$newName];
177 $setSection[$fieldname] = $field;
178 $this->mFlatFields
[$fieldname] = $field;
181 $this->mFieldTree
= $loadedDescriptor;
185 * Add the HTMLForm-specific JavaScript, if it hasn't been
187 * @deprecated since 1.18 load modules with ResourceLoader instead
189 static function addJS() { }
192 * Initialise a new Object for the field
193 * @param $fieldname string
194 * @param $descriptor string input Descriptor, as described above
195 * @return HTMLFormField subclass
197 static function loadInputFromParameters( $fieldname, $descriptor ) {
198 if ( isset( $descriptor['class'] ) ) {
199 $class = $descriptor['class'];
200 } elseif ( isset( $descriptor['type'] ) ) {
201 $class = self
::$typeMappings[$descriptor['type']];
202 $descriptor['class'] = $class;
208 throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
211 $descriptor['fieldname'] = $fieldname;
213 $obj = new $class( $descriptor );
219 * Prepare form for submission
221 function prepareForm() {
222 # Check if we have the info we need
223 if ( !$this->mTitle
instanceof Title
&& $this->mTitle
!== false ) {
224 throw new MWException( "You must call setTitle() on an HTMLForm" );
227 # Load data from the request.
232 * Try submitting, with edit token check first
233 * @return Status|boolean
235 function tryAuthorizedSubmit() {
236 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
239 if ( $this->getMethod() != 'post' ||
$this->getUser()->matchEditToken( $editToken ) ) {
240 $result = $this->trySubmit();
246 * The here's-one-I-made-earlier option: do the submission if
247 * posted, or display the form with or without funky valiation
249 * @return Bool or Status whether submission was successful.
252 $this->prepareForm();
254 $result = $this->tryAuthorizedSubmit();
255 if ( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ){
259 $this->displayForm( $result );
264 * Validate all the fields, and call the submision callback
265 * function if everything is kosher.
266 * @return Mixed Bool true == Successful submission, Bool false
267 * == No submission attempted, anything else == Error to
270 function trySubmit() {
271 # Check for validation
272 foreach ( $this->mFlatFields
as $fieldname => $field ) {
273 if ( !empty( $field->mParams
['nodata'] ) ) {
276 if ( $field->validate(
277 $this->mFieldData
[$fieldname],
281 return isset( $this->mValidationErrorMessage
)
282 ?
$this->mValidationErrorMessage
283 : array( 'htmlform-invalid-input' );
287 $callback = $this->mSubmitCallback
;
289 $data = $this->filterDataForSubmit( $this->mFieldData
);
291 $res = call_user_func( $callback, $data, $this );
297 * Set a callback to a function to do something with the form
298 * once it's been successfully validated.
299 * @param $cb String function name. The function will be passed
300 * the output from HTMLForm::filterDataForSubmit, and must
301 * return Bool true on success, Bool false if no submission
302 * was attempted, or String HTML output to display on error.
304 function setSubmitCallback( $cb ) {
305 $this->mSubmitCallback
= $cb;
309 * Set a message to display on a validation error.
310 * @param $msg Mixed String or Array of valid inputs to wfMsgExt()
311 * (so each entry can be either a String or Array)
313 function setValidationErrorMessage( $msg ) {
314 $this->mValidationErrorMessage
= $msg;
318 * Set the introductory message, overwriting any existing message.
319 * @param $msg String complete text of message to display
321 function setIntro( $msg ) {
322 $this->setPreText( $msg );
326 * Set the introductory message, overwriting any existing message.
328 * @param $msg String complete text of message to display
330 function setPreText( $msg ) { $this->mPre
= $msg; }
333 * Add introductory text.
334 * @param $msg String complete text of message to display
336 function addPreText( $msg ) { $this->mPre
.= $msg; }
339 * Add header text, inside the form.
340 * @param $msg String complete text of message to display
341 * @param $section The section to add the header to
343 function addHeaderText( $msg, $section = null ) {
344 if ( is_null( $section ) ) {
345 $this->mHeader
.= $msg;
347 if ( !isset( $this->mSectionHeaders
[$section] ) ) {
348 $this->mSectionHeaders
[$section] = '';
350 $this->mSectionHeaders
[$section] .= $msg;
355 * Set header text, inside the form.
357 * @param $msg String complete text of message to display
358 * @param $section The section to add the header to
360 function setHeaderText( $msg, $section = null ) {
361 if ( is_null( $section ) ) {
362 $this->mHeader
= $msg;
364 $this->mSectionHeaders
[$section] = $msg;
369 * Add footer text, inside the form.
370 * @param $msg String complete text of message to display
371 * @param $section string The section to add the footer text to
373 function addFooterText( $msg, $section = null ) {
374 if ( is_null( $section ) ) {
375 $this->mFooter
.= $msg;
377 if ( !isset( $this->mSectionFooters
[$section] ) ) {
378 $this->mSectionFooters
[$section] = '';
380 $this->mSectionFooters
[$section] .= $msg;
385 * Set footer text, inside the form.
387 * @param $msg String complete text of message to display
388 * @param $section string The section to add the footer text to
390 function setFooterText( $msg, $section = null ) {
391 if ( is_null( $section ) ) {
392 $this->mFooter
= $msg;
394 $this->mSectionFooters
[$section] = $msg;
399 * Add text to the end of the display.
400 * @param $msg String complete text of message to display
402 function addPostText( $msg ) { $this->mPost
.= $msg; }
405 * Set text at the end of the display.
406 * @param $msg String complete text of message to display
408 function setPostText( $msg ) { $this->mPost
= $msg; }
411 * Add a hidden field to the output
412 * @param $name String field name. This will be used exactly as entered
413 * @param $value String field value
414 * @param $attribs Array
416 public function addHiddenField( $name, $value, $attribs = array() ) {
417 $attribs +
= array( 'name' => $name );
418 $this->mHiddenFields
[] = array( $value, $attribs );
421 public function addButton( $name, $value, $id = null, $attribs = null ) {
422 $this->mButtons
[] = compact( 'name', 'value', 'id', 'attribs' );
426 * Display the form (sending to wgOut), with an appropriate error
427 * message or stack of messages, and any validation errors, etc.
428 * @param $submitResult Mixed output from HTMLForm::trySubmit()
430 function displayForm( $submitResult ) {
431 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
435 * Returns the raw HTML generated by the form
436 * @param $submitResult Mixed output from HTMLForm::trySubmit()
439 function getHTML( $submitResult ) {
440 # For good measure (it is the default)
441 $this->getOutput()->preventClickjacking();
442 $this->getOutput()->addModules( 'mediawiki.htmlform' );
445 . $this->getErrors( $submitResult )
448 . $this->getHiddenFields()
449 . $this->getButtons()
453 $html = $this->wrapForm( $html );
455 return '' . $this->mPre
. $html . $this->mPost
;
459 * Wrap the form innards in an actual <form> element
460 * @param $html String HTML contents to wrap.
461 * @return String wrapped HTML.
463 function wrapForm( $html ) {
465 # Include a <fieldset> wrapper for style, if requested.
466 if ( $this->mWrapperLegend
!== false ) {
467 $html = Xml
::fieldset( $this->mWrapperLegend
, $html );
469 # Use multipart/form-data
470 $encType = $this->mUseMultipart
471 ?
'multipart/form-data'
472 : 'application/x-www-form-urlencoded';
475 'action' => $this->getTitle()->getFullURL(),
476 'method' => $this->mMethod
,
477 'class' => 'visualClear',
478 'enctype' => $encType,
480 if ( !empty( $this->mId
) ) {
481 $attribs['id'] = $this->mId
;
484 return Html
::rawElement( 'form', $attribs, $html );
488 * Get the hidden fields that should go inside the form.
489 * @return String HTML.
491 function getHiddenFields() {
492 global $wgUsePathInfo;
495 if( $this->getMethod() == 'post' ){
496 $html .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
497 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
500 if ( !$wgUsePathInfo && $this->getMethod() == 'get' ) {
501 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
504 foreach ( $this->mHiddenFields
as $data ) {
505 list( $value, $attribs ) = $data;
506 $html .= Html
::hidden( $attribs['name'], $value, $attribs ) . "\n";
513 * Get the submit and (potentially) reset buttons.
514 * @return String HTML.
516 function getButtons() {
520 if ( isset( $this->mSubmitID
) ) {
521 $attribs['id'] = $this->mSubmitID
;
524 if ( isset( $this->mSubmitName
) ) {
525 $attribs['name'] = $this->mSubmitName
;
528 if ( isset( $this->mSubmitTooltip
) ) {
529 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
532 $attribs['class'] = 'mw-htmlform-submit';
534 $html .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
536 if ( $this->mShowReset
) {
537 $html .= Html
::element(
541 'value' => wfMsg( 'htmlform-reset' )
546 foreach ( $this->mButtons
as $button ) {
549 'name' => $button['name'],
550 'value' => $button['value']
553 if ( $button['attribs'] ) {
554 $attrs +
= $button['attribs'];
557 if ( isset( $button['id'] ) ) {
558 $attrs['id'] = $button['id'];
561 $html .= Html
::element( 'input', $attrs );
568 * Get the whole body of the form.
572 return $this->displaySection( $this->mFieldTree
);
576 * Format and display an error message stack.
577 * @param $errors String|Array|Status
580 function getErrors( $errors ) {
581 if ( $errors instanceof Status
) {
582 if ( $errors->isOK() ) {
585 $errorstr = $this->getOutput()->parse( $errors->getWikiText() );
587 } elseif ( is_array( $errors ) ) {
588 $errorstr = $this->formatErrors( $errors );
594 ? Html
::rawElement( 'div', array( 'class' => 'error' ), $errorstr )
599 * Format a stack of error messages into a single HTML string
600 * @param $errors Array of message keys/values
601 * @return String HTML, a <ul> list of errors
603 public static function formatErrors( $errors ) {
606 foreach ( $errors as $error ) {
607 if ( is_array( $error ) ) {
608 $msg = array_shift( $error );
614 $errorstr .= Html
::rawElement(
617 wfMsgExt( $msg, array( 'parseinline' ), $error )
621 $errorstr = Html
::rawElement( 'ul', array(), $errorstr );
627 * Set the text for the submit button
628 * @param $t String plaintext.
630 function setSubmitText( $t ) {
631 $this->mSubmitText
= $t;
635 * Set the text for the submit button to a message
637 * @param $msg String message key
639 public function setSubmitTextMsg( $msg ) {
640 return $this->setSubmitText( $this->msg( $msg )->escaped() );
644 * Get the text for the submit button, either customised or a default.
645 * @return unknown_type
647 function getSubmitText() {
648 return $this->mSubmitText
650 : wfMsg( 'htmlform-submit' );
653 public function setSubmitName( $name ) {
654 $this->mSubmitName
= $name;
657 public function setSubmitTooltip( $name ) {
658 $this->mSubmitTooltip
= $name;
662 * Set the id for the submit button.
664 * @todo FIXME: Integrity of $t is *not* validated
666 function setSubmitID( $t ) {
667 $this->mSubmitID
= $t;
670 public function setId( $id ) {
674 * Prompt the whole form to be wrapped in a <fieldset>, with
675 * this text as its <legend> element.
676 * @param $legend String HTML to go inside the <legend> element.
679 public function setWrapperLegend( $legend ) { $this->mWrapperLegend
= $legend; }
682 * Prompt the whole form to be wrapped in a <fieldset>, with
683 * this message as its <legend> element.
685 * @param $msg String message key
687 public function setWrapperLegendMsg( $msg ) {
688 return $this->setWrapperLegend( $this->msg( $msg )->escaped() );
692 * Set the prefix for various default messages
693 * TODO: currently only used for the <fieldset> legend on forms
694 * with multiple sections; should be used elsewhre?
697 function setMessagePrefix( $p ) {
698 $this->mMessagePrefix
= $p;
702 * Set the title for form submission
703 * @param $t Title of page the form is on/should be posted to
705 function setTitle( $t ) {
713 function getTitle() {
714 return $this->mTitle
=== false
715 ?
$this->getContext()->getTitle()
720 * Set the method used to submit the form
721 * @param $method String
723 public function setMethod( $method='post' ){
724 $this->mMethod
= $method;
727 public function getMethod(){
728 return $this->mMethod
;
733 * @param $fields array[]|HTMLFormField[] array of fields (either arrays or objects)
734 * @param $sectionName string ID attribute of the <table> tag for this section, ignored if empty
735 * @param $fieldsetIDPrefix string ID prefix for the <fieldset> tag of each subsection, ignored if empty
738 function displaySection( $fields, $sectionName = '', $fieldsetIDPrefix = '' ) {
740 $subsectionHtml = '';
741 $hasLeftColumn = false;
743 foreach ( $fields as $key => $value ) {
744 if ( is_object( $value ) ) {
745 $v = empty( $value->mParams
['nodata'] )
746 ?
$this->mFieldData
[$key]
747 : $value->getDefault();
748 $tableHtml .= $value->getTableRow( $v );
750 if ( $value->getLabel() != ' ' ) {
751 $hasLeftColumn = true;
753 } elseif ( is_array( $value ) ) {
754 $section = $this->displaySection( $value, $key );
755 $legend = $this->getLegend( $key );
756 if ( isset( $this->mSectionHeaders
[$key] ) ) {
757 $section = $this->mSectionHeaders
[$key] . $section;
759 if ( isset( $this->mSectionFooters
[$key] ) ) {
760 $section .= $this->mSectionFooters
[$key];
762 $attributes = array();
763 if ( $fieldsetIDPrefix ) {
764 $attributes['id'] = Sanitizer
::escapeId( "$fieldsetIDPrefix$key" );
766 $subsectionHtml .= Xml
::fieldset( $legend, $section, $attributes ) . "\n";
772 if ( !$hasLeftColumn ) { // Avoid strange spacing when no labels exist
773 $classes[] = 'mw-htmlform-nolabel';
777 'class' => implode( ' ', $classes ),
780 if ( $sectionName ) {
781 $attribs['id'] = Sanitizer
::escapeId( "mw-htmlform-$sectionName" );
784 $tableHtml = Html
::rawElement( 'table', $attribs,
785 Html
::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n";
787 if ( $this->mSubSectionBeforeFields
) {
788 return $subsectionHtml . "\n" . $tableHtml;
790 return $tableHtml . "\n" . $subsectionHtml;
795 * Construct the form fields from the Descriptor array
797 function loadData() {
798 $fieldData = array();
800 foreach ( $this->mFlatFields
as $fieldname => $field ) {
801 if ( !empty( $field->mParams
['nodata'] ) ) {
803 } elseif ( !empty( $field->mParams
['disabled'] ) ) {
804 $fieldData[$fieldname] = $field->getDefault();
806 $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() );
811 foreach ( $fieldData as $name => &$value ) {
812 $field = $this->mFlatFields
[$name];
813 $value = $field->filter( $value, $this->mFlatFields
);
816 $this->mFieldData
= $fieldData;
820 * Stop a reset button being shown for this form
821 * @param $suppressReset Bool set to false to re-enable the
824 function suppressReset( $suppressReset = true ) {
825 $this->mShowReset
= !$suppressReset;
829 * Overload this if you want to apply special filtration routines
830 * to the form as a whole, after it's submitted but before it's
833 * @return unknown_type
835 function filterDataForSubmit( $data ) {
840 * Get a string to go in the <legend> of a section fieldset. Override this if you
841 * want something more complicated
845 public function getLegend( $key ) {
846 return wfMsg( "{$this->mMessagePrefix}-$key" );
850 * Returns an array of fields in the form
852 * @return HTMLFormField[]
854 public function getFlatFields() {
855 return $this->mFlatFields
;
860 * The parent class to generate form fields. Any field type should
861 * be a subclass of this.
863 abstract class HTMLFormField
{
865 protected $mValidationCallback;
866 protected $mFilterCallback;
869 protected $mLabel; # String label. Set on construction
871 protected $mClass = '';
880 * This function must be implemented to return the HTML to generate
881 * the input object itself. It should not implement the surrounding
882 * table cells/rows, or labels/help messages.
883 * @param $value String the value to set the input to; eg a default
884 * text for a text input.
885 * @return String valid HTML.
887 abstract function getInputHTML( $value );
890 * Override this function to add specific validation checks on the
891 * field input. Don't forget to call parent::validate() to ensure
892 * that the user-defined callback mValidationCallback is still run
893 * @param $value String the value the field was submitted with
894 * @param $alldata Array the data collected from the form
895 * @return Mixed Bool true on success, or String error to display.
897 function validate( $value, $alldata ) {
898 if ( isset( $this->mParams
['required'] ) && $value === '' ) {
899 return wfMsgExt( 'htmlform-required', 'parseinline' );
902 if ( isset( $this->mValidationCallback
) ) {
903 return call_user_func( $this->mValidationCallback
, $value, $alldata, $this->mParent
);
909 function filter( $value, $alldata ) {
910 if ( isset( $this->mFilterCallback
) ) {
911 $value = call_user_func( $this->mFilterCallback
, $value, $alldata, $this->mParent
);
918 * Should this field have a label, or is there no input element with the
919 * appropriate id for the label to point to?
921 * @return bool True to output a label, false to suppress
923 protected function needsLabel() {
928 * Get the value that this input has been set to from a posted form,
929 * or the input's default value if it has not been set.
930 * @param $request WebRequest
931 * @return String the value
933 function loadDataFromRequest( $request ) {
934 if ( $request->getCheck( $this->mName
) ) {
935 return $request->getText( $this->mName
);
937 return $this->getDefault();
942 * Initialise the object
943 * @param $params array Associative Array. See HTMLForm doc for syntax.
945 function __construct( $params ) {
946 $this->mParams
= $params;
948 # Generate the label from a message, if possible
949 if ( isset( $params['label-message'] ) ) {
950 $msgInfo = $params['label-message'];
952 if ( is_array( $msgInfo ) ) {
953 $msg = array_shift( $msgInfo );
959 $this->mLabel
= wfMsgExt( $msg, 'parseinline', $msgInfo );
960 } elseif ( isset( $params['label'] ) ) {
961 $this->mLabel
= $params['label'];
964 $this->mName
= "wp{$params['fieldname']}";
965 if ( isset( $params['name'] ) ) {
966 $this->mName
= $params['name'];
969 $validName = Sanitizer
::escapeId( $this->mName
);
970 if ( $this->mName
!= $validName && !isset( $params['nodata'] ) ) {
971 throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__
);
974 $this->mID
= "mw-input-{$this->mName}";
976 if ( isset( $params['default'] ) ) {
977 $this->mDefault
= $params['default'];
980 if ( isset( $params['id'] ) ) {
982 $validId = Sanitizer
::escapeId( $id );
984 if ( $id != $validId ) {
985 throw new MWException( "Invalid id '$id' passed to " . __METHOD__
);
991 if ( isset( $params['cssclass'] ) ) {
992 $this->mClass
= $params['cssclass'];
995 if ( isset( $params['validation-callback'] ) ) {
996 $this->mValidationCallback
= $params['validation-callback'];
999 if ( isset( $params['filter-callback'] ) ) {
1000 $this->mFilterCallback
= $params['filter-callback'];
1005 * Get the complete table row for the input, including help text,
1006 * labels, and whatever.
1007 * @param $value String the value to set the input to.
1008 * @return String complete HTML table row.
1010 function getTableRow( $value ) {
1011 # Check for invalid data.
1013 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
1015 $cellAttributes = array();
1016 $verticalLabel = false;
1018 if ( !empty($this->mParams
['vertical-label']) ) {
1019 $cellAttributes['colspan'] = 2;
1020 $verticalLabel = true;
1023 if ( $errors === true ||
( !$this->mParent
->getRequest()->wasPosted() && ( $this->mParent
->getMethod() == 'post' ) ) ) {
1027 $errors = self
::formatErrors( $errors );
1028 $errorClass = 'mw-htmlform-invalid-input';
1031 $label = $this->getLabelHtml( $cellAttributes );
1032 $field = Html
::rawElement(
1034 array( 'class' => 'mw-input' ) +
$cellAttributes,
1035 $this->getInputHTML( $value ) . "\n$errors"
1038 $fieldType = get_class( $this );
1040 if ( $verticalLabel ) {
1041 $html = Html
::rawElement( 'tr',
1042 array( 'class' => 'mw-htmlform-vertical-label' ), $label );
1043 $html .= Html
::rawElement( 'tr',
1044 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
1047 $html = Html
::rawElement( 'tr',
1048 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
1054 if ( isset( $this->mParams
['help-message'] ) ) {
1055 $msg = wfMessage( $this->mParams
['help-message'] );
1056 if ( $msg->exists() ) {
1057 $helptext = $msg->parse();
1059 } elseif ( isset( $this->mParams
['help-messages'] ) ) {
1060 # help-message can be passed a message key (string) or an array containing
1061 # a message key and additional parameters. This makes it impossible to pass
1062 # an array of message key
1063 foreach( $this->mParams
['help-messages'] as $name ) {
1064 $msg = wfMessage( $name );
1065 if( $msg->exists() ) {
1066 $helptext .= $msg->parse(); // append message
1069 } elseif ( isset( $this->mParams
['help'] ) ) {
1070 $helptext = $this->mParams
['help'];
1073 if ( !is_null( $helptext ) ) {
1074 $row = Html
::rawElement( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
1076 $row = Html
::rawElement( 'tr', array(), $row );
1084 * Returns the HTML name of the Field
1088 public function getName() {
1089 return $this->mName
;
1092 function getLabel() {
1093 return $this->mLabel
;
1095 function getLabelHtml( $cellAttributes = array() ) {
1096 # Don't output a for= attribute for labels with no associated input.
1097 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1100 if ( $this->needsLabel() ) {
1101 $for['for'] = $this->mID
;
1104 return Html
::rawElement( 'td', array( 'class' => 'mw-label' ) +
$cellAttributes,
1105 Html
::rawElement( 'label', $for, $this->getLabel() )
1109 function getDefault() {
1110 if ( isset( $this->mDefault
) ) {
1111 return $this->mDefault
;
1118 * Returns the attributes required for the tooltip and accesskey.
1120 * @return array Attributes
1122 public function getTooltipAndAccessKey() {
1123 if ( empty( $this->mParams
['tooltip'] ) ) {
1126 return Linker
::tooltipAndAccesskeyAttribs( $this->mParams
['tooltip'] );
1130 * flatten an array of options to a single array, for instance,
1131 * a set of <options> inside <optgroups>.
1132 * @param $options Associative Array with values either Strings
1134 * @return Array flattened input
1136 public static function flattenOptions( $options ) {
1137 $flatOpts = array();
1139 foreach ( $options as $value ) {
1140 if ( is_array( $value ) ) {
1141 $flatOpts = array_merge( $flatOpts, self
::flattenOptions( $value ) );
1143 $flatOpts[] = $value;
1151 * Formats one or more errors as accepted by field validation-callback.
1152 * @param $errors String|Message|Array of strings or Message instances
1153 * @return String html
1156 protected static function formatErrors( $errors ) {
1157 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1158 $errors = array_shift( $errors );
1161 if ( is_array( $errors ) ) {
1163 foreach ( $errors as $error ) {
1164 if ( $error instanceof Message
) {
1165 $lines[] = Html
::rawElement( 'li', array(), $error->parse() );
1167 $lines[] = Html
::rawElement( 'li', array(), $error );
1170 return Html
::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
1172 if ( $errors instanceof Message
) {
1173 $errors = $errors->parse();
1175 return Html
::rawElement( 'span', array( 'class' => 'error' ), $errors );
1180 class HTMLTextField
extends HTMLFormField
{
1181 function getSize() {
1182 return isset( $this->mParams
['size'] )
1183 ?
$this->mParams
['size']
1187 function getInputHTML( $value ) {
1190 'name' => $this->mName
,
1191 'size' => $this->getSize(),
1193 ) +
$this->getTooltipAndAccessKey();
1195 if ( isset( $this->mParams
['maxlength'] ) ) {
1196 $attribs['maxlength'] = $this->mParams
['maxlength'];
1199 if ( !empty( $this->mParams
['disabled'] ) ) {
1200 $attribs['disabled'] = 'disabled';
1203 # TODO: Enforce pattern, step, required, readonly on the server side as
1205 foreach ( array( 'min', 'max', 'pattern', 'title', 'step',
1206 'placeholder' ) as $param ) {
1207 if ( isset( $this->mParams
[$param] ) ) {
1208 $attribs[$param] = $this->mParams
[$param];
1212 foreach ( array( 'required', 'autofocus', 'multiple', 'readonly' ) as $param ) {
1213 if ( isset( $this->mParams
[$param] ) ) {
1214 $attribs[$param] = '';
1218 # Implement tiny differences between some field variants
1219 # here, rather than creating a new class for each one which
1220 # is essentially just a clone of this one.
1221 if ( isset( $this->mParams
['type'] ) ) {
1222 switch ( $this->mParams
['type'] ) {
1224 $attribs['type'] = 'email';
1227 $attribs['type'] = 'number';
1230 $attribs['type'] = 'number';
1231 $attribs['step'] = 'any';
1236 $attribs['type'] = $this->mParams
['type'];
1241 return Html
::element( 'input', $attribs );
1244 class HTMLTextAreaField
extends HTMLFormField
{
1245 function getCols() {
1246 return isset( $this->mParams
['cols'] )
1247 ?
$this->mParams
['cols']
1251 function getRows() {
1252 return isset( $this->mParams
['rows'] )
1253 ?
$this->mParams
['rows']
1257 function getInputHTML( $value ) {
1260 'name' => $this->mName
,
1261 'cols' => $this->getCols(),
1262 'rows' => $this->getRows(),
1263 ) +
$this->getTooltipAndAccessKey();
1266 if ( !empty( $this->mParams
['disabled'] ) ) {
1267 $attribs['disabled'] = 'disabled';
1270 if ( !empty( $this->mParams
['readonly'] ) ) {
1271 $attribs['readonly'] = 'readonly';
1274 foreach ( array( 'required', 'autofocus' ) as $param ) {
1275 if ( isset( $this->mParams
[$param] ) ) {
1276 $attribs[$param] = '';
1280 return Html
::element( 'textarea', $attribs, $value );
1285 * A field that will contain a numeric value
1287 class HTMLFloatField
extends HTMLTextField
{
1288 function getSize() {
1289 return isset( $this->mParams
['size'] )
1290 ?
$this->mParams
['size']
1294 function validate( $value, $alldata ) {
1295 $p = parent
::validate( $value, $alldata );
1297 if ( $p !== true ) {
1301 $value = trim( $value );
1303 # http://dev.w3.org/html5/spec/common-microsyntaxes.html#real-numbers
1304 # with the addition that a leading '+' sign is ok.
1305 if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
1306 return wfMsgExt( 'htmlform-float-invalid', 'parse' );
1309 # The "int" part of these message names is rather confusing.
1310 # They make equal sense for all numbers.
1311 if ( isset( $this->mParams
['min'] ) ) {
1312 $min = $this->mParams
['min'];
1314 if ( $min > $value ) {
1315 return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
1319 if ( isset( $this->mParams
['max'] ) ) {
1320 $max = $this->mParams
['max'];
1322 if ( $max < $value ) {
1323 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
1332 * A field that must contain a number
1334 class HTMLIntField
extends HTMLFloatField
{
1335 function validate( $value, $alldata ) {
1336 $p = parent
::validate( $value, $alldata );
1338 if ( $p !== true ) {
1342 # http://dev.w3.org/html5/spec/common-microsyntaxes.html#signed-integers
1343 # with the addition that a leading '+' sign is ok. Note that leading zeros
1344 # are fine, and will be left in the input, which is useful for things like
1345 # phone numbers when you know that they are integers (the HTML5 type=tel
1346 # input does not require its value to be numeric). If you want a tidier
1347 # value to, eg, save in the DB, clean it up with intval().
1348 if ( !preg_match( '/^((\+|\-)?\d+)?$/', trim( $value ) )
1350 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
1360 class HTMLCheckField
extends HTMLFormField
{
1361 function getInputHTML( $value ) {
1362 if ( !empty( $this->mParams
['invert'] ) ) {
1366 $attr = $this->getTooltipAndAccessKey();
1367 $attr['id'] = $this->mID
;
1369 if ( !empty( $this->mParams
['disabled'] ) ) {
1370 $attr['disabled'] = 'disabled';
1373 return Xml
::check( $this->mName
, $value, $attr ) . ' ' .
1374 Html
::rawElement( 'label', array( 'for' => $this->mID
), $this->mLabel
);
1378 * For a checkbox, the label goes on the right hand side, and is
1379 * added in getInputHTML(), rather than HTMLFormField::getRow()
1382 function getLabel() {
1387 * @param $request WebRequest
1390 function loadDataFromRequest( $request ) {
1392 if ( isset( $this->mParams
['invert'] ) && $this->mParams
['invert'] ) {
1396 // GetCheck won't work like we want for checks.
1397 // Fetch the value in either one of the two following case:
1398 // - we have a valid token (form got posted or GET forged by the user)
1399 // - checkbox name has a value (false or true), ie is not null
1400 if ( $request->getCheck( 'wpEditToken' ) ||
$request->getVal( $this->mName
)!== null ) {
1401 // XOR has the following truth table, which is what we want
1402 // INVERT VALUE | OUTPUT
1403 // true true | false
1404 // false true | true
1405 // false false | false
1406 // true false | true
1407 return $request->getBool( $this->mName
) xor $invert;
1409 return $this->getDefault();
1415 * A select dropdown field. Basically a wrapper for Xmlselect class
1417 class HTMLSelectField
extends HTMLFormField
{
1418 function validate( $value, $alldata ) {
1419 $p = parent
::validate( $value, $alldata );
1421 if ( $p !== true ) {
1425 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
1427 if ( in_array( $value, $validOptions ) )
1430 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1433 function getInputHTML( $value ) {
1434 $select = new XmlSelect( $this->mName
, $this->mID
, strval( $value ) );
1436 # If one of the options' 'name' is int(0), it is automatically selected.
1437 # because PHP sucks and thinks int(0) == 'some string'.
1438 # Working around this by forcing all of them to strings.
1439 foreach( $this->mParams
['options'] as &$opt ){
1440 if( is_int( $opt ) ){
1441 $opt = strval( $opt );
1444 unset( $opt ); # PHP keeps $opt around as a reference, which is a bit scary
1446 if ( !empty( $this->mParams
['disabled'] ) ) {
1447 $select->setAttribute( 'disabled', 'disabled' );
1450 $select->addOptions( $this->mParams
['options'] );
1452 return $select->getHTML();
1457 * Select dropdown field, with an additional "other" textbox.
1459 class HTMLSelectOrOtherField
extends HTMLTextField
{
1460 static $jsAdded = false;
1462 function __construct( $params ) {
1463 if ( !in_array( 'other', $params['options'], true ) ) {
1464 $msg = isset( $params['other'] ) ?
$params['other'] : wfMsg( 'htmlform-selectorother-other' );
1465 $params['options'][$msg] = 'other';
1468 parent
::__construct( $params );
1471 static function forceToStringRecursive( $array ) {
1472 if ( is_array( $array ) ) {
1473 return array_map( array( __CLASS__
, 'forceToStringRecursive' ), $array );
1475 return strval( $array );
1479 function getInputHTML( $value ) {
1480 $valInSelect = false;
1482 if ( $value !== false ) {
1483 $valInSelect = in_array(
1485 HTMLFormField
::flattenOptions( $this->mParams
['options'] )
1489 $selected = $valInSelect ?
$value : 'other';
1491 $opts = self
::forceToStringRecursive( $this->mParams
['options'] );
1493 $select = new XmlSelect( $this->mName
, $this->mID
, $selected );
1494 $select->addOptions( $opts );
1496 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
1498 $tbAttribs = array( 'id' => $this->mID
. '-other', 'size' => $this->getSize() );
1500 if ( !empty( $this->mParams
['disabled'] ) ) {
1501 $select->setAttribute( 'disabled', 'disabled' );
1502 $tbAttribs['disabled'] = 'disabled';
1505 $select = $select->getHTML();
1507 if ( isset( $this->mParams
['maxlength'] ) ) {
1508 $tbAttribs['maxlength'] = $this->mParams
['maxlength'];
1511 $textbox = Html
::input(
1512 $this->mName
. '-other',
1513 $valInSelect ?
'' : $value,
1518 return "$select<br />\n$textbox";
1522 * @param $request WebRequest
1525 function loadDataFromRequest( $request ) {
1526 if ( $request->getCheck( $this->mName
) ) {
1527 $val = $request->getText( $this->mName
);
1529 if ( $val == 'other' ) {
1530 $val = $request->getText( $this->mName
. '-other' );
1535 return $this->getDefault();
1541 * Multi-select field
1543 class HTMLMultiSelectField
extends HTMLFormField
{
1545 public function __construct( $params ){
1546 parent
::__construct( $params );
1547 if( isset( $params['flatlist'] ) ){
1548 $this->mClass
.= ' mw-htmlform-multiselect-flatlist';
1552 function validate( $value, $alldata ) {
1553 $p = parent
::validate( $value, $alldata );
1555 if ( $p !== true ) {
1559 if ( !is_array( $value ) ) {
1563 # If all options are valid, array_intersect of the valid options
1564 # and the provided options will return the provided options.
1565 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
1567 $validValues = array_intersect( $value, $validOptions );
1568 if ( count( $validValues ) == count( $value ) ) {
1571 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1575 function getInputHTML( $value ) {
1576 $html = $this->formatOptions( $this->mParams
['options'], $value );
1581 function formatOptions( $options, $value ) {
1586 if ( !empty( $this->mParams
['disabled'] ) ) {
1587 $attribs['disabled'] = 'disabled';
1590 foreach ( $options as $label => $info ) {
1591 if ( is_array( $info ) ) {
1592 $html .= Html
::rawElement( 'h1', array(), $label ) . "\n";
1593 $html .= $this->formatOptions( $info, $value );
1595 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
1597 $checkbox = Xml
::check(
1598 $this->mName
. '[]',
1599 in_array( $info, $value, true ),
1600 $attribs +
$thisAttribs );
1601 $checkbox .= ' ' . Html
::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
1603 $html .= ' ' . Html
::rawElement( 'div', array( 'class' => 'mw-htmlform-multiselect-item' ), $checkbox );
1611 * @param $request WebRequest
1614 function loadDataFromRequest( $request ) {
1615 if ( $this->mParent
->getMethod() == 'post' ) {
1616 if( $request->wasPosted() ){
1617 # Checkboxes are just not added to the request arrays if they're not checked,
1618 # so it's perfectly possible for there not to be an entry at all
1619 return $request->getArray( $this->mName
, array() );
1621 # That's ok, the user has not yet submitted the form, so show the defaults
1622 return $this->getDefault();
1625 # This is the impossible case: if we look at $_GET and see no data for our
1626 # field, is it because the user has not yet submitted the form, or that they
1627 # have submitted it with all the options unchecked? We will have to assume the
1628 # latter, which basically means that you can't specify 'positive' defaults
1631 return $request->getArray( $this->mName
, array() );
1635 function getDefault() {
1636 if ( isset( $this->mDefault
) ) {
1637 return $this->mDefault
;
1643 protected function needsLabel() {
1649 * Double field with a dropdown list constructed from a system message in the format
1652 * * New Optgroup header
1653 * Plus a text field underneath for an additional reason. The 'value' of the field is
1654 * ""<select>: <extra reason>"", or "<extra reason>" if nothing has been selected in the
1656 * @todo FIXME: If made 'required', only the text field should be compulsory.
1658 class HTMLSelectAndOtherField
extends HTMLSelectField
{
1660 function __construct( $params ) {
1661 if ( array_key_exists( 'other', $params ) ) {
1662 } elseif( array_key_exists( 'other-message', $params ) ){
1663 $params['other'] = wfMessage( $params['other-message'] )->plain();
1665 $params['other'] = null;
1668 if ( array_key_exists( 'options', $params ) ) {
1669 # Options array already specified
1670 } elseif( array_key_exists( 'options-message', $params ) ){
1671 # Generate options array from a system message
1672 $params['options'] = self
::parseMessage(
1673 wfMessage( $params['options-message'] )->inContentLanguage()->plain(),
1678 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
1680 $this->mFlatOptions
= self
::flattenOptions( $params['options'] );
1682 parent
::__construct( $params );
1686 * Build a drop-down box from a textual list.
1687 * @param $string String message text
1688 * @param $otherName String name of "other reason" option
1690 * TODO: this is copied from Xml::listDropDown(), deprecate/avoid duplication?
1692 public static function parseMessage( $string, $otherName=null ) {
1693 if( $otherName === null ){
1694 $otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
1698 $options = array( $otherName => 'other' );
1700 foreach ( explode( "\n", $string ) as $option ) {
1701 $value = trim( $option );
1702 if ( $value == '' ) {
1704 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
1705 # A new group is starting...
1706 $value = trim( substr( $value, 1 ) );
1708 } elseif ( substr( $value, 0, 2) == '**' ) {
1710 $opt = trim( substr( $value, 2 ) );
1711 if( $optgroup === false ){
1712 $options[$opt] = $opt;
1714 $options[$optgroup][$opt] = $opt;
1717 # groupless reason list
1719 $options[$option] = $option;
1726 function getInputHTML( $value ) {
1727 $select = parent
::getInputHTML( $value[1] );
1729 $textAttribs = array(
1730 'id' => $this->mID
. '-other',
1731 'size' => $this->getSize(),
1734 foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
1735 if ( isset( $this->mParams
[$param] ) ) {
1736 $textAttribs[$param] = '';
1740 $textbox = Html
::input(
1741 $this->mName
. '-other',
1747 return "$select<br />\n$textbox";
1751 * @param $request WebRequest
1752 * @return Array( <overall message>, <select value>, <text field value> )
1754 function loadDataFromRequest( $request ) {
1755 if ( $request->getCheck( $this->mName
) ) {
1757 $list = $request->getText( $this->mName
);
1758 $text = $request->getText( $this->mName
. '-other' );
1760 if ( $list == 'other' ) {
1762 } elseif( !in_array( $list, $this->mFlatOptions
) ){
1763 # User has spoofed the select form to give an option which wasn't
1764 # in the original offer. Sulk...
1766 } elseif( $text == '' ) {
1769 $final = $list . wfMsgForContent( 'colon-separator' ) . $text;
1773 $final = $this->getDefault();
1777 foreach ( $this->mFlatOptions
as $option ) {
1778 $match = $option . wfMsgForContent( 'colon-separator' );
1779 if( strpos( $text, $match ) === 0 ) {
1781 $text = substr( $text, strlen( $match ) );
1786 return array( $final, $list, $text );
1789 function getSize() {
1790 return isset( $this->mParams
['size'] )
1791 ?
$this->mParams
['size']
1795 function validate( $value, $alldata ) {
1796 # HTMLSelectField forces $value to be one of the options in the select
1797 # field, which is not useful here. But we do want the validation further up
1799 $p = parent
::validate( $value[1], $alldata );
1801 if ( $p !== true ) {
1805 if( isset( $this->mParams
['required'] ) && $value[1] === '' ){
1806 return wfMsgExt( 'htmlform-required', 'parseinline' );
1814 * Radio checkbox fields.
1816 class HTMLRadioField
extends HTMLFormField
{
1817 function __construct( $params ) {
1818 parent
::__construct( $params );
1819 if ( isset( $params['flatlist'] ) ) {
1820 $this->mClass
.= ' mw-htmlform-radio-flatlist';
1825 function validate( $value, $alldata ) {
1826 $p = parent
::validate( $value, $alldata );
1828 if ( $p !== true ) {
1832 if ( !is_string( $value ) && !is_int( $value ) ) {
1836 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
1838 if ( in_array( $value, $validOptions ) ) {
1841 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1846 * This returns a block of all the radio options, in one cell.
1847 * @see includes/HTMLFormField#getInputHTML()
1848 * @param $value String
1851 function getInputHTML( $value ) {
1852 $html = $this->formatOptions( $this->mParams
['options'], $value );
1857 function formatOptions( $options, $value ) {
1861 if ( !empty( $this->mParams
['disabled'] ) ) {
1862 $attribs['disabled'] = 'disabled';
1865 # TODO: should this produce an unordered list perhaps?
1866 foreach ( $options as $label => $info ) {
1867 if ( is_array( $info ) ) {
1868 $html .= Html
::rawElement( 'h1', array(), $label ) . "\n";
1869 $html .= $this->formatOptions( $info, $value );
1871 $id = Sanitizer
::escapeId( $this->mID
. "-$info" );
1872 $radio = Xml
::radio(
1876 $attribs +
array( 'id' => $id )
1878 $radio .= ' ' .
1879 Html
::rawElement( 'label', array( 'for' => $id ), $label );
1881 $html .= ' ' . Html
::rawElement( 'div', array( 'class' => 'mw-htmlform-radio-item' ), $radio );
1888 protected function needsLabel() {
1894 * An information field (text blob), not a proper input.
1896 class HTMLInfoField
extends HTMLFormField
{
1897 function __construct( $info ) {
1898 $info['nodata'] = true;
1900 parent
::__construct( $info );
1903 function getInputHTML( $value ) {
1904 return !empty( $this->mParams
['raw'] ) ?
$value : htmlspecialchars( $value );
1907 function getTableRow( $value ) {
1908 if ( !empty( $this->mParams
['rawrow'] ) ) {
1912 return parent
::getTableRow( $value );
1915 protected function needsLabel() {
1920 class HTMLHiddenField
extends HTMLFormField
{
1921 public function __construct( $params ) {
1922 parent
::__construct( $params );
1924 # Per HTML5 spec, hidden fields cannot be 'required'
1925 # http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state
1926 unset( $this->mParams
['required'] );
1929 public function getTableRow( $value ) {
1932 $params['id'] = $this->mID
;
1935 $this->mParent
->addHiddenField(
1944 public function getInputHTML( $value ) { return ''; }
1948 * Add a submit button inline in the form (as opposed to
1949 * HTMLForm::addButton(), which will add it at the end).
1951 class HTMLSubmitField
extends HTMLFormField
{
1953 function __construct( $info ) {
1954 $info['nodata'] = true;
1955 parent
::__construct( $info );
1958 function getInputHTML( $value ) {
1959 return Xml
::submitButton(
1962 'class' => 'mw-htmlform-submit',
1963 'name' => $this->mName
,
1969 protected function needsLabel() {
1974 * Button cannot be invalid
1975 * @param $value String
1976 * @param $alldata Array
1979 public function validate( $value, $alldata ){
1984 class HTMLEditTools
extends HTMLFormField
{
1985 public function getInputHTML( $value ) {
1989 public function getTableRow( $value ) {
1990 if ( empty( $this->mParams
['message'] ) ) {
1991 $msg = wfMessage( 'edittools' );
1993 $msg = wfMessage( $this->mParams
['message'] );
1994 if ( $msg->isDisabled() ) {
1995 $msg = wfMessage( 'edittools' );
1998 $msg->inContentLanguage();
2001 return '<tr><td></td><td class="mw-input">'
2002 . '<div class="mw-editTools">'
2003 . $msg->parseAsBlock()
2004 . "</div></td></tr>\n";