6use InvalidArgumentException;
31 protected $mValidationCallback;
33 protected $mFilterCallback;
140 if ( $this->mParent ) {
141 return $this->mParent->msg( $key, ...
$params );
180 $cloner = $this->mParams[
'cloner'] ??
null;
182 $field = $cloner->findNearestField( $this, $name );
188 if ( $backCompat && str_starts_with( $name,
'wp' ) &&
189 !$this->mParent->hasField( $name )
192 return $this->mParent->getField( substr( $name, 2 ) );
194 return $this->mParent->getField( $name );
211 $cloner = $field->mParams[
'cloner'] ??
null;
213 $value = $cloner->extractFieldData( $field, $alldata );
218 $value = $alldata[$field->mParams[
'fieldname']] ?? $field->getDefault();
222 if ( $asDisplay && $field instanceof
HTMLCheckField && ( $field->mParams[
'invert'] ??
false ) ) {
253 $makeException =
function (
string $details ) use ( $origParams ): InvalidArgumentException {
254 return new InvalidArgumentException(
255 "Invalid hide-if or disable-if specification for $this->mName: " .
256 $details .
" in " . var_export( $origParams,
true )
262 if ( count(
$params ) !== 1 ) {
263 throw $makeException(
"NOT takes exactly one parameter" );
271 foreach (
$params as $i => $p ) {
272 if ( !is_array( $p ) ) {
273 $type = get_debug_type( $p );
274 throw $makeException(
"Expected array, found $type at index $i" );
282 if ( count(
$params ) !== 2 ) {
283 throw $makeException(
"$op takes exactly two parameters" );
286 if ( !is_string( $name ) || !is_string( $value ) ) {
287 throw $makeException(
"Parameters for $op must be strings" );
292 throw $makeException(
"Unknown operation" );
305 $valueChk = [
'AND' =>
false,
'OR' =>
true,
'NAND' =>
false,
'NOR' => true ];
306 $valueRet = [
'AND' =>
true,
'OR' =>
false,
'NAND' =>
false,
'NOR' => true ];
315 return !$valueRet[$op];
318 return $valueRet[$op];
329 return ( $value === $testValue );
331 return ( $value !== $testValue );
365 return [ $op, $field->getName(), $value ];
376 foreach ( $this->mCondState as $type =>
$params ) {
391 return isset( $this->mCondState[
'hide'] ) &&
404 return ( $this->mParams[
'disabled'] ??
false ) ||
406 ( isset( $this->mCondState[
'disable'] )
438 if ( $this->
isHidden( $alldata ) ) {
442 if ( isset( $this->mParams[
'required'] )
443 && $this->mParams[
'required'] !==
false
444 && ( $value ===
'' || $value ===
false || $value ===
null )
446 return $this->
msg(
'htmlform-required' );
449 if ( !isset( $this->mValidationCallback ) ) {
453 $p = ( $this->mValidationCallback )( $value, $alldata, $this->mParent );
456 $language = $this->mParent ? $this->mParent->getLanguage() : RequestContext::getMain()->getLanguage();
458 return $p->isGood() ? true : Status::wrap( $p )->getHTML(
false,
false, $language );
472 public function filter( $value, $alldata ) {
473 if ( isset( $this->mFilterCallback ) ) {
474 $value = ( $this->mFilterCallback )( $value, $alldata, $this->mParent );
501 $this->mShowEmptyLabels = $show;
519 || $request->
getCheck(
'wpFormIdentifier' );
531 if ( $request->getCheck( $this->mName ) ) {
532 return $request->getText( $this->mName );
550 $this->mParent =
$params[
'parent'];
556 __METHOD__ .
": Constructing an HTMLFormField without a 'parent' parameter",
561 # Generate the label from a message, if possible
562 if ( isset(
$params[
'label-message'] ) ) {
564 } elseif ( isset(
$params[
'label'] ) ) {
565 if (
$params[
'label'] ===
' ' ||
$params[
'label'] ===
"\u{00A0}" ) {
567 $this->mLabel =
"\u{00A0}";
569 $this->mLabel = htmlspecialchars(
$params[
'label'] );
571 } elseif ( isset(
$params[
'label-raw'] ) ) {
572 $this->mLabel =
$params[
'label-raw'];
577 if ( isset(
$params[
'dir'] ) ) {
581 $this->mID =
"mw-input-{$this->mName}";
583 if ( isset(
$params[
'default'] ) ) {
584 $this->mDefault =
$params[
'default'];
587 if ( isset(
$params[
'id'] ) ) {
591 if ( isset(
$params[
'cssclass'] ) ) {
592 $this->mClass =
$params[
'cssclass'];
595 if ( isset(
$params[
'csshelpclass'] ) ) {
596 $this->mHelpClass =
$params[
'csshelpclass'];
599 if ( isset(
$params[
'validation-callback'] ) ) {
600 $this->mValidationCallback =
$params[
'validation-callback'];
603 if ( isset(
$params[
'filter-callback'] ) ) {
604 $this->mFilterCallback =
$params[
'filter-callback'];
607 if ( isset(
$params[
'hidelabel'] ) ) {
608 $this->mShowEmptyLabels =
false;
610 if ( isset(
$params[
'notices'] ) ) {
611 $this->mNotices =
$params[
'notices'];
616 $this->mCondState[
'hide'] =
$params[
'hide-if'];
617 $this->mCondStateClass[] =
'mw-htmlform-hide-if';
623 $this->mCondState[
'disable'] =
$params[
'disable-if'];
624 $this->mCondStateClass[] =
'mw-htmlform-disable-if';
642 $cellAttributes = [];
646 if ( !empty( $this->mParams[
'vertical-label'] ) ) {
647 $cellAttributes[
'colspan'] = 2;
648 $verticalLabel =
true;
650 $verticalLabel =
false;
655 $field = Html::rawElement(
657 [
'class' =>
'mw-input' ] + $cellAttributes,
658 $inputHtml .
"\n$errors"
661 if ( $this->mCondState ) {
663 $rowClasses .= implode(
' ', $this->mCondStateClass );
666 if ( $verticalLabel ) {
667 $html = Html::rawElement(
'tr',
668 $rowAttributes + [
'class' =>
"mw-htmlform-vertical-label $rowClasses" ], $label );
669 $html .= Html::rawElement(
'tr',
671 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
675 $html = Html::rawElement(
'tr',
677 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
682 return $html . $helptext;
700 $cellAttributes = [];
705 'mw-htmlform-nolabel' => ( $label ===
'' )
708 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
710 if ( $horizontalLabel ) {
711 $field =
"\u{00A0}" . $inputHtml .
"\n$errors";
713 $field = Html::rawElement(
716 [
'class' => $outerDivClass ] + $cellAttributes,
717 $inputHtml .
"\n$errors"
721 $wrapperAttributes = [
'class' => [
722 "mw-htmlform-field-$fieldType",
727 if ( $this->mCondState ) {
729 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
731 return Html::rawElement(
'div', $wrapperAttributes, $label . $field ) .
747 if ( !$inputField ) {
752 new \OOUI\Widget( [
'content' =>
new \OOUI\HtmlSnippet( $this->
getDiv( $value ) ) ] ),
758 if ( is_string( $inputField ) ) {
762 $inputField = new \OOUI\Widget( [
'content' =>
new \OOUI\HtmlSnippet( $inputField ) ] );
769 foreach ( $errors as &$error ) {
770 $error = new \OOUI\HtmlSnippet( $error );
774 'classes' => [
"mw-htmlform-field-$fieldType" ],
776 'help' => ( $help !==
null && $help !==
'' ) ?
new \OOUI\HtmlSnippet( $help ) :
null,
778 'infusable' => $infusable,
780 'notices' => $this->mNotices ?: [],
782 if ( $this->mClass !==
'' ) {
786 $preloadModules =
false;
789 $preloadModules =
true;
790 $config[
'classes'][] =
'mw-htmlform-autoinfuse';
792 if ( $this->mCondState ) {
793 $config[
'classes'] = array_merge( $config[
'classes'], $this->mCondStateClass );
798 if ( $label && $label !==
"\u{00A0}" && $label !==
' ' ) {
799 $config[
'label'] = new \OOUI\HtmlSnippet( $label );
802 if ( $this->mCondState ) {
803 $preloadModules =
true;
809 if ( $preloadModules ) {
810 $this->mParent->getOutput()->addModules(
'mediawiki.htmlform.ooui' );
811 $this->mParent->getOutput()->addModules( $this->
getOOUIModules() );
825 $isDisabled = ( $this->mParams[
'disabled'] ?? false );
829 $labelValue = trim( $this->
getLabel() );
832 if ( $labelValue !==
'' && $labelValue !==
"\u{00A0}" && $labelValue !==
' ' ) {
834 $labelClasses = [
'cdx-label' ];
836 $labelClasses[] =
'cdx-label--disabled';
839 $labelDiv = Html::rawElement(
'div', [
'class' => $labelClasses ],
841 Html::rawElement(
'label', [
'class' =>
'cdx-label__label' ] + $labelFor,
843 Html::rawElement(
'span', [
'class' =>
'cdx-label__label__text' ],
858 $validationMessage =
'';
860 if ( $errors !==
'' ) {
861 $validationMessage = Html::rawElement(
'div', [
'class' =>
'cdx-field__validation-message' ],
869 $controlClasses = [
'cdx-field__control' ];
871 $controlClasses[] =
'cdx-field__control--has-help-text';
873 $control = Html::rawElement(
'div', [
'class' => $controlClasses ], $inputHtml );
877 "mw-htmlform-field-{$this->getClassName()}",
883 $fieldClasses[] =
'cdx-field--disabled';
885 $fieldAttributes = [];
887 if ( $this->mCondState ) {
889 $fieldClasses = array_merge( $fieldClasses, $this->mCondStateClass );
892 return Html::rawElement(
'div', [
'class' => $fieldClasses ] + $fieldAttributes,
893 $labelDiv . $control . $helptext . $validationMessage
905 $name = explode(
'\\', static::class );
938 return !$this->
isHelpInline() && $this->getHelpMessages();
964 return "\n" . $errors .
981 $this->mVFormClass =
' mw-ui-vform-field';
982 return $this->
getDiv( $value );
994 return "\n" . $errors .
1009 if ( $helptext ===
null ) {
1013 $rowAttributes = [];
1014 if ( $this->mCondState ) {
1019 $tdClasses = [
'htmlform-tip' ];
1020 if ( $this->mHelpClass !==
false ) {
1023 return Html::rawElement(
'tr', $rowAttributes,
1024 Html::rawElement(
'td', [
'colspan' => 2,
'class' => $tdClasses ], $helptext )
1038 if ( $helptext ===
null ) {
1042 $wrapperAttributes = [
1043 'class' => array_merge( $cssClasses, [
'htmlform-tip' ] ),
1045 if ( $this->mHelpClass !==
false ) {
1048 if ( $this->mCondState ) {
1050 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
1052 return Html::rawElement(
'div', $wrapperAttributes, $helptext );
1066 private function getHelpMessages(): array {
1067 if ( isset( $this->mParams[
'help-message'] ) ) {
1068 return [ $this->mParams[
'help-message'] ];
1069 } elseif ( isset( $this->mParams[
'help-messages'] ) ) {
1070 return $this->mParams[
'help-messages'];
1071 } elseif ( isset( $this->mParams[
'help-raw'] ) ) {
1072 return [
new HtmlArmor( $this->mParams[
'help-raw'] ) ];
1073 } elseif ( isset( $this->mParams[
'help'] ) ) {
1075 return [
new HtmlArmor( $this->mParams[
'help'] ) ];
1090 foreach ( $this->getHelpMessages() as $msg ) {
1092 $html[] = HtmlArmor::getHtml( $msg );
1094 $msg = $this->getMessage( $msg );
1095 if ( $msg->exists() ) {
1096 $html[] = $msg->parse();
1101 return $html ? implode( $this->msg(
'word-separator' )->escaped(), $html ) :
null;
1113 return $this->mParams[
'help-inline'] ??
true;
1129 $errors = $this->validate( $value, $this->mParent->mFieldData );
1131 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1135 return [ self::formatErrors( $errors ),
'mw-htmlform-invalid-input' ];
1146 $errors = $this->validate( $value, $this->mParent->mFieldData );
1148 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1152 if ( !is_array( $errors ) ) {
1153 $errors = [ $errors ];
1155 foreach ( $errors as &$error ) {
1156 if ( $error instanceof
Message ) {
1157 $error = $error->parse();
1169 return $this->mLabel ??
'';
1179 # Don't output a for= attribute for labels with no associated input.
1180 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1181 $for = $this->needsLabel() ? [
'for' => $this->mID ] : [];
1183 $labelValue = trim( $this->getLabel() );
1184 $hasLabel = $labelValue !==
'' && $labelValue !==
"\u{00A0}" && $labelValue !==
' ';
1186 $displayFormat = $this->mParent->getDisplayFormat();
1187 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
1189 if ( $displayFormat ===
'table' ) {
1190 return Html::rawElement(
'td',
1191 [
'class' =>
'mw-label' ] + $cellAttributes,
1192 Html::rawElement(
'label', $for, $labelValue ) );
1193 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
1194 if ( $displayFormat ===
'div' && !$horizontalLabel ) {
1195 return Html::rawElement(
'div',
1196 [
'class' =>
'mw-label' ] + $cellAttributes,
1197 Html::rawElement(
'label', $for, $labelValue ) );
1199 return Html::rawElement(
'label', $for, $labelValue );
1211 return $this->mDefault ??
null;
1220 if ( empty( $this->mParams[
'tooltip'] ) ) {
1224 return Linker::tooltipAndAccesskeyAttribs( $this->mParams[
'tooltip'] );
1233 if ( empty( $this->mParams[
'tooltip'] ) ) {
1238 'title' => Linker::titleAttrib( $this->mParams[
'tooltip'] ),
1239 'accessKey' => Linker::accesskey( $this->mParams[
'tooltip'] ),
1251 static $boolAttribs = [
'disabled',
'required',
'autofocus',
'multiple',
'readonly' ];
1254 foreach ( $list as $key ) {
1255 if ( in_array( $key, $boolAttribs ) ) {
1256 if ( !empty( $this->mParams[$key] ) ) {
1259 } elseif ( isset( $this->mParams[$key] ) ) {
1260 $ret[$key] = $this->mParams[$key];
1276 private function lookupOptionsKeys( $options, $needsParse ) {
1278 foreach ( $options as $key => $value ) {
1279 $msg = $this->msg( $key );
1280 $msgAsText = $needsParse ? $msg->parse() : $msg->plain();
1281 if ( array_key_exists( $msgAsText, $ret ) ) {
1282 LoggerFactory::getInstance(
'error' )->error(
1283 'The option that uses the message key {msg_key_one} has the same translation as ' .
1284 'another option in {lang}. This means that {msg_key_one} will not be used as an option.',
1286 'msg_key_one' => $key,
1287 'lang' => $this->mParent ?
1288 $this->mParent->getLanguageCode()->toBcp47Code() :
1294 $ret[$msgAsText] = is_array( $value )
1295 ? $this->lookupOptionsKeys( $value, $needsParse )
1309 if ( is_array( $array ) ) {
1310 return array_map( [ __CLASS__,
'forceToStringRecursive' ], $array );
1312 return strval( $array );
1323 if ( $this->mOptions ===
false ) {
1324 if ( array_key_exists(
'options-messages', $this->mParams ) ) {
1325 $needsParse = $this->mParams[
'options-messages-parse'] ??
false;
1326 if ( $needsParse ) {
1327 $this->mOptionsLabelsNotFromMessage =
true;
1329 $this->mOptions = $this->lookupOptionsKeys( $this->mParams[
'options-messages'], $needsParse );
1330 } elseif ( array_key_exists(
'options', $this->mParams ) ) {
1331 $this->mOptionsLabelsNotFromMessage =
true;
1332 $this->mOptions = self::forceToStringRecursive( $this->mParams[
'options'] );
1333 } elseif ( array_key_exists(
'options-message', $this->mParams ) ) {
1334 $message = $this->getMessage( $this->mParams[
'options-message'] )->inContentLanguage()->plain();
1335 $this->mOptions = Html::listDropdownOptions( $message );
1337 $this->mOptions =
null;
1341 return $this->mOptions;
1350 $oldoptions = $this->getOptions();
1352 if ( $oldoptions ===
null ) {
1356 return Html::listDropdownOptionsOoui( $oldoptions );
1369 foreach ( $options as $value ) {
1370 if ( is_array( $value ) ) {
1371 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1373 $flatOpts[] = $value;
1394 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1395 $errors = array_shift( $errors );
1398 if ( is_array( $errors ) ) {
1399 foreach ( $errors as &$error ) {
1400 $error = Html::rawElement(
'li', [],
1404 $errors = Html::rawElement(
'ul', [], implode(
"\n", $errors ) );
1405 } elseif ( $errors instanceof
Message ) {
1406 $errors = $errors->parse();
1409 return Html::errorBox( $errors );
1421 if ( $this->mParent ) {
1422 $message->setContext( $this->mParent );
1436 return !empty( $this->mParams[
'nodata'] );
1448 return (
bool)$this->mCondState;
1453class_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.