* @file
*/
-use Wikimedia\ObjectFactory;
-
/**
* Object handling generic submission, CSRF protection, layout and
* other logic for UI forms. in a reusable manner.
protected $mMessagePrefix;
/** @var HTMLFormField[] */
- protected $mFlatFields;
-
- protected $mFieldTree;
+ protected $mFlatFields = [];
+ protected $mFieldTree = [];
protected $mShowReset = false;
protected $mShowSubmit = true;
+ /** @var string[] */
protected $mSubmitFlags = [ 'primary', 'progressive' ];
protected $mShowCancel = false;
protected $mCancelTarget;
switch ( $displayFormat ) {
case 'vform':
- return ObjectFactory::constructClassInstance( VFormHTMLForm::class, $arguments );
+ return new VFormHTMLForm( ...$arguments );
case 'ooui':
- return ObjectFactory::constructClassInstance( OOUIHTMLForm::class, $arguments );
+ return new OOUIHTMLForm( ...$arguments );
default:
- /** @var HTMLForm $form */
- $form = ObjectFactory::constructClassInstance( self::class, $arguments );
+ $form = new self( ...$arguments );
$form->setDisplayFormat( $displayFormat );
return $form;
}
/**
* Build a new HTMLForm from an array of field attributes
*
- * @param array $descriptor Array of Field constructs, as described above
+ * @param array $descriptor Array of Field constructs, as described
+ * in the class documentation
* @param IContextSource|null $context Available since 1.18, will become compulsory in 1.18.
* Obviates the need to call $form->setTitle()
* @param string $messagePrefix A prefix to go in front of default messages
$this->displayFormat = 'div';
}
- // Expand out into a tree.
+ $this->addFields( $descriptor );
+ }
+
+ /**
+ * Add fields to the form
+ *
+ * @since 1.34
+ *
+ * @param array $descriptor Array of Field constructs, as described
+ * in the class documentation
+ * @return HTMLForm
+ */
+ public function addFields( $descriptor ) {
$loadedDescriptor = [];
- $this->mFlatFields = [];
foreach ( $descriptor as $fieldname => $info ) {
+
$section = $info['section'] ?? '';
if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
$this->mFlatFields[$fieldname] = $field;
}
- $this->mFieldTree = $loadedDescriptor;
+ $this->mFieldTree = array_merge( $this->mFieldTree, $loadedDescriptor );
+
+ return $this;
}
/**
* @since 1.23
*
* @param string $fieldname Name of the field
- * @param array &$descriptor Input Descriptor, as described above
+ * @param array &$descriptor Input Descriptor, as described
+ * in the class documentation
*
* @throws MWException
* @return string Name of a HTMLFormField subclass
* Initialise a new Object for the field
*
* @param string $fieldname Name of the field
- * @param array $descriptor Input Descriptor, as described above
+ * @param array $descriptor Input Descriptor, as described
+ * in the class documentation
* @param HTMLForm|null $parent Parent instance of HTMLForm
*
* @throws MWException
}
/**
- * Set whether the HTML form can be collapsed.
+ * Enable collapsible mode, and set whether the form is collapsed by default.
*
* @since 1.34
- * @param bool $collapsedByDefault (optional) whether the form is collapsed by default
+ * @param bool $collapsedByDefault Whether the form is collapsed by default (optional).
* @return HTMLForm $this for chaining calls
*/
public function setCollapsibleOptions( $collapsedByDefault = false ) {
return $this->displaySection( $this->mFieldTree, $this->mTableId );
}
- /**
- * Format and display an error message stack.
- *
- * @param string|array|Status $errors
- *
- * @deprecated since 1.28, use getErrorsOrWarnings() instead
- *
- * @return string
- */
- public function getErrors( $errors ) {
- wfDeprecated( __METHOD__, '1.28' );
- return $this->getErrorsOrWarnings( $errors, 'error' );
- }
-
/**
* Returns a formatted list of errors or warnings from the given elements.
*