Merge "Fix HTMLForm noData logic in trySubmit"
[lhc/web/wiklou.git] / includes / htmlform / HTMLForm.php
index 6f88975..0dab3bb 100644 (file)
@@ -569,7 +569,7 @@ class HTMLForm extends ContextSource {
 
                # Check for cancelled submission
                foreach ( $this->mFlatFields as $fieldname => $field ) {
-                       if ( !empty( $field->mParams['nodata'] ) ) {
+                       if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
                                continue;
                        }
                        if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
@@ -580,7 +580,7 @@ class HTMLForm extends ContextSource {
 
                # Check for validation
                foreach ( $this->mFlatFields as $fieldname => $field ) {
-                       if ( !empty( $field->mParams['nodata'] ) ) {
+                       if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
                                continue;
                        }
                        if ( $field->isHidden( $this->mFieldData ) ) {
@@ -1117,7 +1117,7 @@ class HTMLForm extends ContextSource {
                        ];
 
                        if ( isset( $button['label-message'] ) ) {
-                               $label = $this->msg( $button['label-message'] )->parse();
+                               $label = $this->getMessage( $button['label-message'] )->parse();
                        } elseif ( isset( $button['label'] ) ) {
                                $label = htmlspecialchars( $button['label'] );
                        } elseif ( isset( $button['label-raw'] ) ) {
@@ -1198,17 +1198,10 @@ class HTMLForm extends ContextSource {
                $errorstr = '';
 
                foreach ( $errors as $error ) {
-                       if ( is_array( $error ) ) {
-                               $msg = array_shift( $error );
-                       } else {
-                               $msg = $error;
-                               $error = [];
-                       }
-
                        $errorstr .= Html::rawElement(
                                'li',
                                [],
-                               $this->msg( $msg, $error )->parse()
+                               $this->getMessage( $error )->parse()
                        );
                }
 
@@ -1474,13 +1467,22 @@ class HTMLForm extends ContextSource {
         * @param string $fieldsetIDPrefix ID prefix for the "<fieldset>" tag of
         *   each subsection, ignored if empty.
         * @param bool &$hasUserVisibleFields Whether the section had user-visible fields.
+        * @throws LogicException When called on uninitialized field data, e.g. When
+        *  HTMLForm::displayForm was called without calling HTMLForm::prepareForm
+        *  first.
         *
         * @return string
         */
        public function displaySection( $fields,
                $sectionName = '',
                $fieldsetIDPrefix = '',
-               &$hasUserVisibleFields = false ) {
+               &$hasUserVisibleFields = false
+       ) {
+               if ( $this->mFieldData === null ) {
+                       throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
+                               . 'You probably called displayForm() without calling prepareForm() first.' );
+               }
+
                $displayFormat = $this->getDisplayFormat();
 
                $html = [];
@@ -1493,7 +1495,7 @@ class HTMLForm extends ContextSource {
 
                foreach ( $fields as $key => $value ) {
                        if ( $value instanceof HTMLFormField ) {
-                               $v = isset( $this->mFieldData[$key] )
+                               $v = array_key_exists( $key, $this->mFieldData )
                                        ? $this->mFieldData[$key]
                                        : $value->getDefault();
 
@@ -1716,4 +1718,14 @@ class HTMLForm extends ContextSource {
 
                return $this;
        }
+
+       /**
+        * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
+        * name + parameters array) into a Message.
+        * @param mixed $value
+        * @return Message
+        */
+       protected function getMessage( $value ) {
+               return Message::newFromSpecifier( $value )->setContext( $this );
+       }
 }