8use HTMLFormFieldCloner;
9use InvalidArgumentException;
31 protected $mValidationCallback;
35 protected $mLabel; # String label, as HTML. Set on construction.
128 if ( $this->mParent ) {
129 return $this->mParent->msg( $key, ...
$params );
168 $cloner = $this->mParams[
'cloner'] ??
null;
170 $field = $cloner->findNearestField( $this, $name );
176 if ( $backCompat && str_starts_with( $name,
'wp' ) &&
177 !$this->mParent->hasField( $name )
180 return $this->mParent->getField( substr( $name, 2 ) );
182 return $this->mParent->getField( $name );
199 $cloner = $field->mParams[
'cloner'] ??
null;
201 $value = $cloner->extractFieldData( $field, $alldata );
206 $value = $alldata[$field->mParams[
'fieldname']] ?? $field->getDefault();
210 if ( $asDisplay && $field instanceof
HTMLCheckField && ( $field->mParams[
'invert'] ??
false ) ) {
241 $makeException =
function (
string $details ) use ( $origParams ): InvalidArgumentException {
242 return new InvalidArgumentException(
243 "Invalid hide-if or disable-if specification for $this->mName: " .
244 $details .
" in " . var_export( $origParams,
true )
250 if ( count(
$params ) !== 1 ) {
251 throw $makeException(
"NOT takes exactly one parameter" );
259 foreach (
$params as $i => $p ) {
260 if ( !is_array( $p ) ) {
261 $type = gettype( $p );
262 throw $makeException(
"Expected array, found $type at index $i" );
270 if ( count(
$params ) !== 2 ) {
271 throw $makeException(
"$op takes exactly two parameters" );
274 if ( !is_string( $name ) || !is_string( $value ) ) {
275 throw $makeException(
"Parameters for $op must be strings" );
280 throw $makeException(
"Unknown operation" );
293 $valueChk = [
'AND' =>
false,
'OR' =>
true,
'NAND' =>
false,
'NOR' => true ];
294 $valueRet = [
'AND' =>
true,
'OR' =>
false,
'NAND' =>
false,
'NOR' => true ];
303 return !$valueRet[$op];
306 return $valueRet[$op];
317 return ( $value === $testValue );
319 return ( $value !== $testValue );
353 return [ $op, $field->getName(), $value ];
364 foreach ( $this->mCondState as $type =>
$params ) {
379 return isset( $this->mCondState[
'hide'] ) &&
392 return ( $this->mParams[
'disabled'] ??
false ) ||
394 ( isset( $this->mCondState[
'disable'] )
426 if ( $this->
isHidden( $alldata ) ) {
430 if ( isset( $this->mParams[
'required'] )
431 && $this->mParams[
'required'] !==
false
432 && ( $value ===
'' || $value ===
false || $value ===
null )
434 return $this->
msg(
'htmlform-required' );
437 if ( !isset( $this->mValidationCallback ) ) {
441 $p = ( $this->mValidationCallback )( $value, $alldata, $this->mParent );
444 $language = $this->mParent ? $this->mParent->getLanguage() : RequestContext::getMain()->getLanguage();
446 return $p->isGood() ? true : Status::wrap( $p )->getHTML(
false,
false, $language );
460 public function filter( $value, $alldata ) {
461 if ( isset( $this->mFilterCallback ) ) {
489 $this->mShowEmptyLabels = $show;
507 || $request->
getCheck(
'wpFormIdentifier' );
519 if ( $request->getCheck( $this->mName ) ) {
520 return $request->getText( $this->mName );
538 $this->mParent =
$params[
'parent'];
544 __METHOD__ .
": Constructing an HTMLFormField without a 'parent' parameter",
549 # Generate the label from a message, if possible
550 if ( isset(
$params[
'label-message'] ) ) {
552 } elseif ( isset(
$params[
'label'] ) ) {
553 if (
$params[
'label'] ===
' ' ||
$params[
'label'] ===
"\u{00A0}" ) {
555 $this->mLabel =
"\u{00A0}";
557 $this->mLabel = htmlspecialchars(
$params[
'label'] );
559 } elseif ( isset(
$params[
'label-raw'] ) ) {
560 $this->mLabel =
$params[
'label-raw'];
565 if ( isset(
$params[
'dir'] ) ) {
569 $this->mID =
"mw-input-{$this->mName}";
571 if ( isset(
$params[
'default'] ) ) {
572 $this->mDefault =
$params[
'default'];
575 if ( isset(
$params[
'id'] ) ) {
579 if ( isset(
$params[
'cssclass'] ) ) {
580 $this->mClass =
$params[
'cssclass'];
583 if ( isset(
$params[
'csshelpclass'] ) ) {
584 $this->mHelpClass =
$params[
'csshelpclass'];
587 if ( isset(
$params[
'validation-callback'] ) ) {
588 $this->mValidationCallback =
$params[
'validation-callback'];
591 if ( isset(
$params[
'filter-callback'] ) ) {
592 $this->mFilterCallback =
$params[
'filter-callback'];
595 if ( isset(
$params[
'hidelabel'] ) ) {
596 $this->mShowEmptyLabels =
false;
598 if ( isset(
$params[
'notices'] ) ) {
599 $this->mNotices =
$params[
'notices'];
604 $this->mCondState[
'hide'] =
$params[
'hide-if'];
605 $this->mCondStateClass[] =
'mw-htmlform-hide-if';
611 $this->mCondState[
'disable'] =
$params[
'disable-if'];
612 $this->mCondStateClass[] =
'mw-htmlform-disable-if';
630 $cellAttributes = [];
634 if ( !empty( $this->mParams[
'vertical-label'] ) ) {
635 $cellAttributes[
'colspan'] = 2;
636 $verticalLabel =
true;
638 $verticalLabel =
false;
643 $field = Html::rawElement(
645 [
'class' =>
'mw-input' ] + $cellAttributes,
646 $inputHtml .
"\n$errors"
649 if ( $this->mCondState ) {
651 $rowClasses .= implode(
' ', $this->mCondStateClass );
654 if ( $verticalLabel ) {
655 $html = Html::rawElement(
'tr',
656 $rowAttributes + [
'class' =>
"mw-htmlform-vertical-label $rowClasses" ], $label );
657 $html .= Html::rawElement(
'tr',
659 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
663 $html = Html::rawElement(
'tr',
665 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
670 return $html . $helptext;
688 $cellAttributes = [];
693 'mw-htmlform-nolabel' => ( $label ===
'' )
696 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
698 if ( $horizontalLabel ) {
699 $field =
"\u{00A0}" . $inputHtml .
"\n$errors";
701 $field = Html::rawElement(
704 [
'class' => $outerDivClass ] + $cellAttributes,
705 $inputHtml .
"\n$errors"
709 $wrapperAttributes = [
'class' => [
710 "mw-htmlform-field-$fieldType",
715 if ( $this->mCondState ) {
717 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
719 return Html::rawElement(
'div', $wrapperAttributes, $label . $field ) .
735 if ( !$inputField ) {
740 new \OOUI\Widget( [
'content' =>
new \OOUI\HtmlSnippet( $this->
getDiv( $value ) ) ] ),
746 if ( is_string( $inputField ) ) {
750 $inputField = new \OOUI\Widget( [
'content' =>
new \OOUI\HtmlSnippet( $inputField ) ] );
757 foreach ( $errors as &$error ) {
758 $error = new \OOUI\HtmlSnippet( $error );
762 'classes' => [
"mw-htmlform-field-$fieldType" ],
764 'help' => ( $help !==
null && $help !==
'' ) ?
new \OOUI\HtmlSnippet( $help ) :
null,
766 'infusable' => $infusable,
768 'notices' => $this->mNotices ?: [],
770 if ( $this->mClass !==
'' ) {
774 $preloadModules =
false;
777 $preloadModules =
true;
778 $config[
'classes'][] =
'mw-htmlform-autoinfuse';
780 if ( $this->mCondState ) {
781 $config[
'classes'] = array_merge( $config[
'classes'], $this->mCondStateClass );
786 if ( $label && $label !==
"\u{00A0}" && $label !==
' ' ) {
787 $config[
'label'] = new \OOUI\HtmlSnippet( $label );
790 if ( $this->mCondState ) {
791 $preloadModules =
true;
797 if ( $preloadModules ) {
798 $this->mParent->getOutput()->addModules(
'mediawiki.htmlform.ooui' );
799 $this->mParent->getOutput()->addModules( $this->
getOOUIModules() );
813 $isDisabled = ( $this->mParams[
'disabled'] ?? false );
817 $labelValue = trim( $this->
getLabel() );
820 if ( $labelValue !==
'' && $labelValue !==
"\u{00A0}" && $labelValue !==
' ' ) {
822 $labelClasses = [
'cdx-label' ];
824 $labelClasses[] =
'cdx-label--disabled';
827 $labelDiv = Html::rawElement(
'div', [
'class' => $labelClasses ],
829 Html::rawElement(
'label', [
'class' =>
'cdx-label__label' ] + $labelFor,
831 Html::rawElement(
'span', [
'class' =>
'cdx-label__label__text' ],
846 $validationMessage =
'';
848 if ( $errors !==
'' ) {
849 $validationMessage = Html::rawElement(
'div', [
'class' =>
'cdx-field__validation-message' ],
857 $controlClasses = [
'cdx-field__control' ];
859 $controlClasses[] =
'cdx-field__control--has-help-text';
861 $control = Html::rawElement(
'div', [
'class' => $controlClasses ], $inputHtml );
865 "mw-htmlform-field-{$this->getClassName()}",
871 $fieldClasses[] =
'cdx-field--disabled';
873 $fieldAttributes = [];
875 if ( $this->mCondState ) {
877 $fieldClasses = array_merge( $fieldClasses, $this->mCondStateClass );
880 return Html::rawElement(
'div', [
'class' => $fieldClasses ] + $fieldAttributes,
881 $labelDiv . $control . $helptext . $validationMessage
893 $name = explode(
'\\', static::class );
926 return !$this->
isHelpInline() && $this->getHelpMessages();
952 return "\n" . $errors .
969 $this->mVFormClass =
' mw-ui-vform-field';
970 return $this->
getDiv( $value );
982 return "\n" . $errors .
997 if ( $helptext ===
null ) {
1001 $rowAttributes = [];
1002 if ( $this->mCondState ) {
1007 $tdClasses = [
'htmlform-tip' ];
1008 if ( $this->mHelpClass !==
false ) {
1011 return Html::rawElement(
'tr', $rowAttributes,
1012 Html::rawElement(
'td', [
'colspan' => 2,
'class' => $tdClasses ], $helptext )
1026 if ( $helptext ===
null ) {
1030 $wrapperAttributes = [
1031 'class' => array_merge( $cssClasses, [
'htmlform-tip' ] ),
1033 if ( $this->mHelpClass !==
false ) {
1036 if ( $this->mCondState ) {
1038 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
1040 return Html::rawElement(
'div', $wrapperAttributes, $helptext );
1054 private function getHelpMessages(): array {
1055 if ( isset( $this->mParams[
'help-message'] ) ) {
1056 return [ $this->mParams[
'help-message'] ];
1057 } elseif ( isset( $this->mParams[
'help-messages'] ) ) {
1058 return $this->mParams[
'help-messages'];
1059 } elseif ( isset( $this->mParams[
'help'] ) ) {
1060 return [
new HtmlArmor( $this->mParams[
'help'] ) ];
1075 foreach ( $this->getHelpMessages() as $msg ) {
1077 $html[] = HtmlArmor::getHtml( $msg );
1079 $msg = $this->getMessage( $msg );
1080 if ( $msg->exists() ) {
1081 $html[] = $msg->parse();
1086 return $html ? implode( $this->msg(
'word-separator' )->escaped(), $html ) :
null;
1098 return $this->mParams[
'help-inline'] ??
true;
1114 $errors = $this->validate( $value, $this->mParent->mFieldData );
1116 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1120 return [ self::formatErrors( $errors ),
'mw-htmlform-invalid-input' ];
1131 $errors = $this->validate( $value, $this->mParent->mFieldData );
1133 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1137 if ( !is_array( $errors ) ) {
1138 $errors = [ $errors ];
1140 foreach ( $errors as &$error ) {
1141 if ( $error instanceof
Message ) {
1142 $error = $error->parse();
1154 return $this->mLabel ??
'';
1164 # Don't output a for= attribute for labels with no associated input.
1165 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1166 $for = $this->needsLabel() ? [
'for' => $this->mID ] : [];
1168 $labelValue = trim( $this->getLabel() );
1169 $hasLabel = $labelValue !==
'' && $labelValue !==
"\u{00A0}" && $labelValue !==
' ';
1171 $displayFormat = $this->mParent->getDisplayFormat();
1172 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
1174 if ( $displayFormat ===
'table' ) {
1175 return Html::rawElement(
'td',
1176 [
'class' =>
'mw-label' ] + $cellAttributes,
1177 Html::rawElement(
'label', $for, $labelValue ) );
1178 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
1179 if ( $displayFormat ===
'div' && !$horizontalLabel ) {
1180 return Html::rawElement(
'div',
1181 [
'class' =>
'mw-label' ] + $cellAttributes,
1182 Html::rawElement(
'label', $for, $labelValue ) );
1184 return Html::rawElement(
'label', $for, $labelValue );
1196 return $this->mDefault ??
null;
1205 if ( empty( $this->mParams[
'tooltip'] ) ) {
1209 return Linker::tooltipAndAccesskeyAttribs( $this->mParams[
'tooltip'] );
1218 if ( empty( $this->mParams[
'tooltip'] ) ) {
1223 'title' => Linker::titleAttrib( $this->mParams[
'tooltip'] ),
1224 'accessKey' => Linker::accesskey( $this->mParams[
'tooltip'] ),
1236 static $boolAttribs = [
'disabled',
'required',
'autofocus',
'multiple',
'readonly' ];
1239 foreach ( $list as $key ) {
1240 if ( in_array( $key, $boolAttribs ) ) {
1241 if ( !empty( $this->mParams[$key] ) ) {
1244 } elseif ( isset( $this->mParams[$key] ) ) {
1245 $ret[$key] = $this->mParams[$key];
1261 private function lookupOptionsKeys( $options, $needsParse ) {
1263 foreach ( $options as $key => $value ) {
1264 $msg = $this->msg( $key );
1265 $msgAsText = $needsParse ? $msg->parse() : $msg->plain();
1266 if ( array_key_exists( $msgAsText, $ret ) ) {
1267 LoggerFactory::getInstance(
'error' )->error(
1268 'The option that uses the message key {msg_key_one} has the same translation as ' .
1269 'another option in {lang}. This means that {msg_key_one} will not be used as an option.',
1271 'msg_key_one' => $key,
1272 'lang' => $this->mParent ?
1273 $this->mParent->getLanguageCode()->toBcp47Code() :
1279 $ret[$msgAsText] = is_array( $value )
1280 ? $this->lookupOptionsKeys( $value, $needsParse )
1294 if ( is_array( $array ) ) {
1295 return array_map( [ __CLASS__,
'forceToStringRecursive' ], $array );
1297 return strval( $array );
1308 if ( $this->mOptions ===
false ) {
1309 if ( array_key_exists(
'options-messages', $this->mParams ) ) {
1310 $needsParse = $this->mParams[
'options-messages-parse'] ??
false;
1311 if ( $needsParse ) {
1312 $this->mOptionsLabelsNotFromMessage =
true;
1314 $this->mOptions = $this->lookupOptionsKeys( $this->mParams[
'options-messages'], $needsParse );
1315 } elseif ( array_key_exists(
'options', $this->mParams ) ) {
1316 $this->mOptionsLabelsNotFromMessage =
true;
1317 $this->mOptions = self::forceToStringRecursive( $this->mParams[
'options'] );
1318 } elseif ( array_key_exists(
'options-message', $this->mParams ) ) {
1319 $message = $this->getMessage( $this->mParams[
'options-message'] )->inContentLanguage()->plain();
1320 $this->mOptions = Html::listDropdownOptions( $message );
1322 $this->mOptions =
null;
1326 return $this->mOptions;
1335 $oldoptions = $this->getOptions();
1337 if ( $oldoptions ===
null ) {
1341 return Html::listDropdownOptionsOoui( $oldoptions );
1354 foreach ( $options as $value ) {
1355 if ( is_array( $value ) ) {
1356 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1358 $flatOpts[] = $value;
1379 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1380 $errors = array_shift( $errors );
1383 if ( is_array( $errors ) ) {
1384 foreach ( $errors as &$error ) {
1385 $error = Html::rawElement(
'li', [],
1389 $errors = Html::rawElement(
'ul', [], implode(
"\n", $errors ) );
1390 } elseif ( $errors instanceof
Message ) {
1391 $errors = $errors->parse();
1394 return Html::errorBox( $errors );
1406 if ( $this->mParent ) {
1407 $message->setContext( $this->mParent );
1421 return !empty( $this->mParams[
'nodata'] );
1433 return (
bool)$this->mCondState;
1438class_alias( HTMLFormField::class,
'HTMLFormField' );
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
if(!defined('MW_SETUP_CALLBACK'))
Marks HTML that shouldn't be escaped.
Group all the pieces relevant to the context of a request into one instance.
Generic operation result class Has warning/error list, boolean status and arbitrary value.