r78246 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78245‎ | r78246 | r78247 >
Date:15:32, 12 December 2010
Author:happy-melon
Status:ok (Comments)
Tags:
Comment:
Follow-up to r64866: follow the HTML5 spec when validating floats and ints, and support 'required' attribute universally (apart from type=hidden).
Modified paths:
  • /trunk/phase3/includes/HTMLForm.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/HTMLForm.php
@@ -16,32 +16,32 @@
1717 * The constructor input is an associative array of $fieldname => $info,
1818 * where $info is an Associative Array with any of the following:
1919 *
20 - * 'class' -- the subclass of HTMLFormField that will be used
21 - * to create the object. *NOT* the CSS class!
22 - * 'type' -- roughly translates into the <select> type attribute.
23 - * if 'class' is not specified, this is used as a map
24 - * through HTMLForm::$typeMappings to get the class name.
25 - * 'default' -- default value when the form is displayed
26 - * 'id' -- HTML id attribute
27 - * 'cssclass' -- CSS class
28 - * 'options' -- varies according to the specific object.
29 - * 'label-message' -- message key for a message to use as the label.
30 - * can be an array of msg key and then parameters to
31 - * the message.
32 - * 'label' -- alternatively, a raw text message. Overridden by
33 - * label-message
34 - * 'help-message' -- message key for a message to use as a help text.
35 - * can be an array of msg key and then parameters to
36 - * the message.
37 - * 'required' -- passed through to the object, indicating that it
38 - * is a required field.
39 - * 'size' -- the length of text fields
40 - * 'filter-callback -- a function name to give you the chance to
41 - * massage the inputted value before it's processed.
42 - * @see HTMLForm::filter()
43 - * 'validation-callback' -- a function name to give you the chance
44 - * to impose extra validation on the field input.
45 - * @see HTMLForm::validate()
 20+ * 'class' -- the subclass of HTMLFormField that will be used
 21+ * to create the object. *NOT* the CSS class!
 22+ * 'type' -- roughly translates into the <select> type attribute.
 23+ * if 'class' is not specified, this is used as a map
 24+ * through HTMLForm::$typeMappings to get the class name.
 25+ * 'default' -- default value when the form is displayed
 26+ * 'id' -- HTML id attribute
 27+ * 'cssclass' -- CSS class
 28+ * 'options' -- varies according to the specific object.
 29+ * 'label-message' -- message key for a message to use as the label.
 30+ * can be an array of msg key and then parameters to
 31+ * the message.
 32+ * 'label' -- alternatively, a raw text message. Overridden by
 33+ * label-message
 34+ * 'help-message' -- message key for a message to use as a help text.
 35+ * can be an array of msg key and then parameters to
 36+ * the message.
 37+ * 'required' -- passed through to the object, indicating that it
 38+ * is a required field.
 39+ * 'size' -- the length of text fields
 40+ * 'filter-callback -- a function name to give you the chance to
 41+ * massage the inputted value before it's processed.
 42+ * @see HTMLForm::filter()
 43+ * 'validation-callback' -- a function name to give you the chance
 44+ * to impose extra validation on the field input.
 45+ * @see HTMLForm::validate()
4646 *
4747 * TODO: Document 'section' / 'subsection' stuff
4848 */
@@ -187,10 +187,10 @@
188188 * The here's-one-I-made-earlier option: do the submission if
189189 * posted, or display the form with or without funky valiation
190190 * errors
191 - * @return Bool whether submission was successful.
 191+ * @return Bool or Status whether submission was successful.
192192 */
193193 function show() {
194 - // Check if we have the info we need
 194+ # Check if we have the info we need
195195 if ( ! $this->mTitle ) {
196196 throw new MWException( "You must call setTitle() on an HTMLForm" );
197197 }
@@ -209,9 +209,7 @@
210210 $result = $this->trySubmit();
211211 }
212212
213 - if ( $result === true ||
214 - ( $result instanceof Status && $result->isGood() ) )
215 - {
 213+ if ( $result === true || ( $result instanceof Status && $result->isGood() ) ){
216214 return $result;
217215 }
218216
@@ -708,6 +706,10 @@
709707 return call_user_func( $this->mValidationCallback, $value, $alldata );
710708 }
711709
 710+ if ( isset( $this->mParams['required'] ) && $value === '' ) {
 711+ return wfMsgExt( 'htmlform-required', 'parseinline' );
 712+ }
 713+
712714 return true;
713715 }
714716
@@ -1001,20 +1003,6 @@
10021004
10031005 return Html::element( 'input', $attribs );
10041006 }
1005 -
1006 - public function validate( $value, $alldata ) {
1007 - $p = parent::validate( $value, $alldata );
1008 -
1009 - if ( $p !== true ) {
1010 - return $p;
1011 - }
1012 -
1013 - if ( isset( $this->mParams['required'] ) && $value === '' ) {
1014 - return wfMsgExt( 'htmlform-required', 'parseinline' );
1015 - }
1016 -
1017 - return true;
1018 - }
10191007 }
10201008 class HTMLTextAreaField extends HTMLFormField {
10211009 function getCols() {
@@ -1054,20 +1042,6 @@
10551043
10561044 return Html::element( 'textarea', $attribs, $value );
10571045 }
1058 -
1059 - public function validate( $value, $alldata ) {
1060 - $p = parent::validate( $value, $alldata );
1061 -
1062 - if ( $p !== true ) {
1063 - return $p;
1064 - }
1065 -
1066 - if ( isset( $this->mParams['required'] ) && $value === '' ) {
1067 - return wfMsgExt( 'htmlform-required', 'parseinline' );
1068 - }
1069 -
1070 - return true;
1071 - }
10721046 }
10731047
10741048 /**
@@ -1086,8 +1060,12 @@
10871061 if ( $p !== true ) {
10881062 return $p;
10891063 }
 1064+
 1065+ $value = trim( $value );
10901066
1091 - if ( floatval( $value ) != $value ) {
 1067+ # http://dev.w3.org/html5/spec/common-microsyntaxes.html#real-numbers
 1068+ # with the addition that a leading '+' sign is ok.
 1069+ if ( !preg_match( '/^(\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?$/i', $value ) ) {
10921070 return wfMsgExt( 'htmlform-float-invalid', 'parse' );
10931071 }
10941072
@@ -1124,8 +1102,13 @@
11251103 return $p;
11261104 }
11271105
1128 - if ( $value !== ''
1129 - && ( !is_numeric( $value ) || round( $value ) != $value )
 1106+ # http://dev.w3.org/html5/spec/common-microsyntaxes.html#signed-integers
 1107+ # with the addition that a leading '+' sign is ok. Note that leading zeros
 1108+ # are fine, and will be left in the input, which is useful for things like
 1109+ # phone numbers when you know that they are integers (the HTML5 type=tel
 1110+ # input does not require its value to be numeric). If you want a tidier
 1111+ # value to, eg, save in the DB, clean it up with intval().
 1112+ if ( !preg_match( '/^(\+|\-)?\d*$/', trim( $value ) )
11301113 ) {
11311114 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
11321115 }
@@ -1346,11 +1329,13 @@
13471330 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
13481331 $html .= $this->formatOptions( $info, $value );
13491332 } else {
1350 - $thisAttribs = array( 'id' => $this->mID . "-$info", 'value' => $info );
 1333+ $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
13511334
1352 - $checkbox = Xml::check( $this->mName . '[]', in_array( $info, $value, true ),
1353 - $attribs + $thisAttribs );
1354 - $checkbox .= '&#160;' . Html::rawElement( 'label', array( 'for' => $this->mID . "-$info" ), $label );
 1335+ $checkbox = Xml::check(
 1336+ $this->mName . '[]',
 1337+ in_array( $info, $value, true ),
 1338+ $attribs + $thisAttribs );
 1339+ $checkbox .= '&#160;' . Html::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
13551340
13561341 $html .= $checkbox . '<br />';
13571342 }
@@ -1490,6 +1475,10 @@
14911476 # forcing the 'wp' prefix on hidden field names
14921477 # is undesirable
14931478 $this->mName = substr( $this->mName, 2 );
 1479+
 1480+ # Per HTML5 spec, hidden fields cannot be 'required'
 1481+ # http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state
 1482+ unset( $this->mParams['required'] );
14941483 }
14951484
14961485 public function getTableRow( $value ) {
@@ -1535,6 +1524,13 @@
15361525 protected function needsLabel() {
15371526 return false;
15381527 }
 1528+
 1529+ /**
 1530+ * Button cannot be invalid
 1531+ */
 1532+ public function validate( $value, $alldata ){
 1533+ return true;
 1534+ }
15391535 }
15401536
15411537 class HTMLEditTools extends HTMLFormField {

Follow-up revisions

RevisionCommit summaryAuthorDate
r78250Follow-up r78246: clean several integer preferences before saving. It's not ...happy-melon15:38, 12 December 2010
r78436Better regexes for r78246.happy-melon13:33, 15 December 2010
r784371.17: Merge tagged revisions from trunk: r77878, r77981, r77982, r77994, r780...catrope14:14, 15 December 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r64866Improvements and fixes to HTMLForm:...happy-melon12:03, 10 April 2010

Comments

#Comment by Catrope (talk | contribs)   13:08, 15 December 2010
+		if ( !preg_match( '/^(\+|\-)?\d*$/', trim( $value ) )

Shouldn't this be \d+ instead of \d*? The regex above lets single plus or minus signs through, as well as the empty string.

OK otherwise, marking as such.

#Comment by Happy-melon (talk | contribs)   13:34, 15 December 2010

The empty string should be allowed. The single sign shouldn't though; improved in r78246.

Status & tagging log