if ( $this->mName ) {
$attribs['name'] = $this->mName;
}
+ if ( $this->needsJSForHtml5FormValidation() ) {
+ $attribs['novalidate'] = true;
+ }
return $attribs;
}
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;
+ }
}
public function skipLoadData( $request ) {
return !empty( $this->mParams['nodata'] );
}
+
+ /**
+ * Whether this field requires the user agent to have JavaScript enabled for the client-side HTML5
+ * form validation to work correctly.
+ *
+ * @return boolean
+ * @since 1.29
+ */
+ public function needsJSForHtml5FormValidation() {
+ if ( $this->mHideIf ) {
+ // This is probably more restrictive than it needs to be, but better safe than sorry
+ return true;
+ }
+ return false;
+ }
}
mw.hook( 'htmlform.enhance' ).fire( $( document ) );
} );
+ mw.hook( 'htmlform.enhance' ).add( function ( $root ) {
+ // Turn HTML5 form validation back on, in cases where it was disabled server-side (see
+ // HTMLForm::needsJSForHtml5FormValidation()) because we need extra logic implemented in JS to
+ // validate correctly. Currently, this is only used for forms containing fields with 'hide-if'.
+ $root.find( '.mw-htmlform' ).removeAttr( 'novalidate' );
+ } );
+
}( mediaWiki, jQuery ) );