53 $this->uniqueId = $this->
getClassName() . ++self::$counter .
'x';
54 parent::__construct( $params );
56 if ( empty( $this->mParams[
'fields'] ) || !is_array( $this->mParams[
'fields'] ) ) {
57 throw new MWException(
'HTMLFormFieldCloner called without any fields' );
62 if ( isset( $this->mParams[
'fields'][
'delete'] ) ) {
63 $class =
'mw-htmlform-cloner-delete-button';
64 $info = $this->mParams[
'fields'][
'delete'] + [
65 'formnovalidate' =>
true,
68 unset( $info[
'name'], $info[
'class'] );
70 if ( !isset( $info[
'type'] ) || $info[
'type'] !==
'submit' ) {
72 'HTMLFormFieldCloner delete field, if specified, must be of type "submit"'
76 if ( !in_array( $class, explode(
' ', $info[
'cssclass'] ) ) ) {
77 $info[
'cssclass'] .=
" $class";
80 $this->mParams[
'fields'][
'delete'] = $info;
93 foreach ( $this->mParams[
'fields'] as $fieldname => $info ) {
94 $name =
"{$this->mName}[$key][$fieldname]";
95 if ( isset( $info[
'name'] ) ) {
96 $info[
'name'] =
"{$this->mName}[$key][{$info['name']}]";
98 $info[
'name'] = $name;
100 if ( isset( $info[
'id'] ) ) {
101 $info[
'id'] = Sanitizer::escapeIdForAttribute(
"{$this->mID}--$key--{$info['id']}" );
103 $info[
'id'] = Sanitizer::escapeIdForAttribute(
"{$this->mID}--$key--$fieldname" );
107 if ( $this->mHideIf ) {
108 if ( isset( $info[
'hide-if'] ) ) {
116 $field = HTMLForm::loadInputFromParameters( $name, $info, $this->mParent );
117 $fields[$fieldname] = $field;
132 foreach ( $values as $fieldname => $value ) {
133 $name =
"{$this->mName}[$key][$fieldname]";
134 $data[$name] = $value;
146 if ( !$request->getCheck(
'wpEditToken' ) && $request->getArray( $this->mName ) ===
null ) {
150 $values = $request->getArray( $this->mName );
151 if ( $values ===
null ) {
156 foreach ( $values as $key => $value ) {
157 if ( $key ===
'create' || isset( $value[
'delete'] ) ) {
169 foreach ( $fields as $fieldname => $field ) {
170 if ( $field->skipLoadData( $subrequest ) ) {
172 } elseif ( !empty( $field->mParams[
'disabled'] ) ) {
173 $row[$fieldname] = $field->getDefault();
175 $row[$fieldname] = $field->loadDataFromRequest( $subrequest );
181 if ( isset( $values[
'create'] ) ) {
185 foreach ( $fields as $fieldname => $field ) {
186 if ( !empty( $field->mParams[
'nodata'] ) ) {
189 $row[$fieldname] = $field->getDefault();
199 $ret = parent::getDefault();
203 if ( $ret ===
null ) {
206 foreach ( $fields as $fieldname => $field ) {
207 if ( !empty( $field->mParams[
'nodata'] ) ) {
210 $row[$fieldname] = $field->getDefault();
224 if ( isset( $values[
'nonjs'] ) ) {
228 foreach ( $values as $key => $value ) {
230 foreach ( $fields as $fieldname => $field ) {
231 if ( !array_key_exists( $fieldname, $value ) ) {
234 if ( $field->cancelSubmit( $value[$fieldname], $alldata ) ) {
240 return parent::cancelSubmit( $values, $alldata );
248 if ( isset( $this->mParams[
'required'] )
249 && $this->mParams[
'required'] !==
false
252 return $this->
msg(
'htmlform-cloner-required' );
255 if ( isset( $values[
'nonjs'] ) ) {
262 foreach ( $values as $key => $value ) {
264 foreach ( $fields as $fieldname => $field ) {
265 if ( !array_key_exists( $fieldname, $value ) ) {
268 if ( $field->isHidden( $alldata ) ) {
271 $ok = $field->validate( $value[$fieldname], $alldata );
272 if ( $ok !==
true ) {
278 return parent::validate( $values, $alldata );
289 $displayFormat = $this->mParams[
'format'] ?? $this->mParent->getDisplayFormat();
292 $getFieldHtmlMethod = $displayFormat ==
'table' ?
'getTableRow' : (
'get' . $displayFormat );
299 foreach ( $fields as $fieldname => $field ) {
300 $v = array_key_exists( $fieldname, $values )
301 ? $values[$fieldname]
302 : $field->getDefault();
306 list( $name, $value, $params ) = $field->getHiddenFieldData( $v );
307 $hidden .= Html::hidden( $name, $value, $params ) .
"\n";
309 $html .= $field->$getFieldHtmlMethod( $v );
311 $labelValue = trim( $field->getLabel() );
312 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
318 if ( !isset( $fields[
'delete'] ) ) {
321 if ( $displayFormat ===
'table' ) {
322 $html .= $field->$getFieldHtmlMethod( $field->getDefault() );
324 $html .= $field->getInputHTML( $field->getDefault() );
328 if ( $displayFormat !==
'raw' ) {
330 'mw-htmlform-cloner-row',
334 $classes[] =
'mw-htmlform-nolabel';
338 'class' => implode(
' ', $classes ),
341 if ( $displayFormat ===
'table' ) {
342 $html = Html::rawElement(
'table',
344 Html::rawElement(
'tbody', [],
"\n$html\n" ) ) .
"\n";
346 $html = Html::rawElement(
'div', $attribs,
"\n$html\n" );
352 if ( !empty( $this->mParams[
'row-legend'] ) ) {
353 $legend = $this->
msg( $this->mParams[
'row-legend'] )->text();
354 $html = Xml::fieldset( $legend, $html );
365 $name =
"{$this->mName}[$key][delete]";
366 $label = $this->mParams[
'delete-button-message'] ??
'htmlform-cloner-delete';
367 $field = HTMLForm::loadInputFromParameters( $name, [
369 'formnovalidate' =>
true,
371 'id' => Sanitizer::escapeIdForAttribute(
"{$this->mID}--$key--delete" ),
372 'cssclass' =>
'mw-htmlform-cloner-delete-button',
373 'default' => $this->
getMessage( $label )->text(),
379 $name =
"{$this->mName}[create]";
380 $label = $this->mParams[
'create-button-message'] ??
'htmlform-cloner-create';
381 return HTMLForm::loadInputFromParameters( $name, [
383 'formnovalidate' =>
true,
385 'id' => Sanitizer::escapeIdForAttribute(
"{$this->mID}--create" ),
386 'cssclass' =>
'mw-htmlform-cloner-create-button',
387 'default' => $this->getMessage( $label )->text(),
394 foreach ( (array)$values as $key => $value ) {
395 if ( $key ===
'nonjs' ) {
398 $html .= Html::rawElement(
'li', [
'class' =>
'mw-htmlform-cloner-li' ],
399 $this->getInputHTMLForKey( $key, $value )
403 $template = $this->getInputHTMLForKey( $this->uniqueId, [] );
404 $html = Html::rawElement(
'ul', [
405 'id' =>
"mw-htmlform-cloner-list-{$this->mID}",
406 'class' =>
'mw-htmlform-cloner-ul',
407 'data-template' => $template,
408 'data-unique-id' => $this->uniqueId,
411 $field = $this->getCreateButtonHtml();
412 $html .= $field->getInputHTML( $field->getDefault() );
428 $fields = $this->createFieldsForKey( $key );
429 foreach ( $fields as $fieldname => $field ) {
430 $v = array_key_exists( $fieldname, $values )
431 ? $values[$fieldname]
432 : $field->getDefault();
436 list( $name, $value, $params ) = $field->getHiddenFieldData( $v );
437 $hidden .= Html::hidden( $name, $value, $params ) .
"\n";
439 $html .= $field->getOOUI( $v );
443 if ( !isset( $fields[
'delete'] ) ) {
444 $field = $this->getDeleteButtonHtml( $key );
445 $fieldHtml = $field->getInputOOUI( $field->getDefault() );
446 $fieldHtml->setInfusable(
true );
452 'mw-htmlform-cloner-row',
456 'class' => implode(
' ', $classes ),
459 $html = Html::rawElement(
'div', $attribs,
"\n$html\n" );
463 if ( !empty( $this->mParams[
'row-legend'] ) ) {
464 $legend = $this->msg( $this->mParams[
'row-legend'] )->text();
465 $html = Xml::fieldset( $legend, $html );
474 foreach ( (array)$values as $key => $value ) {
475 if ( $key ===
'nonjs' ) {
478 $html .= Html::rawElement(
'li', [
'class' =>
'mw-htmlform-cloner-li' ],
479 $this->getInputOOUIForKey( $key, $value )
483 $template = $this->getInputOOUIForKey( $this->uniqueId, [] );
484 $html = Html::rawElement(
'ul', [
485 'id' =>
"mw-htmlform-cloner-list-{$this->mID}",
486 'class' =>
'mw-htmlform-cloner-ul',
487 'data-template' => $template,
488 'data-unique-id' => $this->uniqueId,
491 $field = $this->getCreateButtonHtml();
492 $fieldHtml = $field->getInputOOUI( $field->getDefault() );
493 $fieldHtml->setInfusable(
true );
Similar to FauxRequest, but only fakes URL parameters and method (POST or GET) and use the base reque...