*/
public function trySubmit() {
$valid = true;
- $hoistedErrors = [];
- $hoistedErrors[] = isset( $this->mValidationErrorMessage )
- ? $this->mValidationErrorMessage
- : [ 'htmlform-invalid-input' ];
+ $hoistedErrors = Status::newGood();
+ if ( $this->mValidationErrorMessage ) {
+ foreach ( (array)$this->mValidationErrorMessage as $error ) {
+ call_user_func_array( [ $hoistedErrors, 'fatal' ], $error );
+ }
+ } else {
+ $hoistedErrors->fatal( 'htmlform-invalid-input' );
+ }
$this->mWasSubmitted = true;
if ( $res !== true ) {
$valid = false;
if ( $res !== false && !$field->canDisplayErrors() ) {
- $hoistedErrors[] = [ 'rawmessage', $res ];
+ if ( is_string( $res ) ) {
+ $hoistedErrors->fatal( 'rawmessage', $res );
+ } else {
+ $hoistedErrors->fatal( $res );
+ }
}
}
}
if ( !$valid ) {
- if ( count( $hoistedErrors ) === 1 ) {
- $hoistedErrors = $hoistedErrors[0];
- }
return $hoistedErrors;
}
: 'application/x-www-form-urlencoded';
# Attributes
$attribs = [
+ 'class' => 'mw-htmlform',
'action' => $this->getAction(),
'method' => $this->getMethod(),
'enctype' => $encType,
if ( $this->mName ) {
$attribs['name'] = $this->mName;
}
+ if ( $this->needsJSForHtml5FormValidation() ) {
+ $attribs['novalidate'] = true;
+ }
return $attribs;
}
return Html::rawElement(
'form',
- $this->getFormAttributes() + [ 'class' => 'visualClear' ],
+ $this->getFormAttributes(),
$html
);
}
protected function getMessage( $value ) {
return Message::newFromSpecifier( $value )->setContext( $this );
}
+
+ /**
+ * Whether this form, with its current fields, requires the user agent to have JavaScript enabled
+ * for the client-side HTML5 form validation to work correctly. If this function returns true, a
+ * 'novalidate' attribute will be added on the `<form>` element. It will be removed if the user
+ * agent has JavaScript support, in htmlform.js.
+ *
+ * @return boolean
+ * @since 1.29
+ */
+ public function needsJSForHtml5FormValidation() {
+ foreach ( $this->mFlatFields as $fieldname => $field ) {
+ if ( $field->needsJSForHtml5FormValidation() ) {
+ return true;
+ }
+ }
+ return false;
+ }
}