Index: trunk/phase3/includes/HTMLForm.php |
— | — | @@ -19,11 +19,12 @@ |
20 | 20 | * |
21 | 21 | * 'class' -- the subclass of HTMLFormField that will be used |
22 | 22 | * to create the object. *NOT* the CSS class! |
23 | | - * 'type' -- roughly translates into the <select> type attribute. |
| 23 | + * 'type' -- roughly translates into the <select> type attribute. |
24 | 24 | * if 'class' is not specified, this is used as a map |
25 | 25 | * through HTMLForm::$typeMappings to get the class name. |
26 | 26 | * 'default' -- default value when the form is displayed |
27 | | - * 'id' -- HTML id attribute |
| 27 | + * 'id' -- HTML id attribute |
| 28 | + * 'cssclass' -- CSS class |
28 | 29 | * 'options' -- varies according to the specific object. |
29 | 30 | * 'label-message' -- message key for a message to use as the label. |
30 | 31 | * can be an array of msg key and then parameters to |
— | — | @@ -35,7 +36,7 @@ |
36 | 37 | * the message. |
37 | 38 | * 'required' -- passed through to the object, indicating that it |
38 | 39 | * is a required field. |
39 | | - * 'size' -- the length of text fields |
| 40 | + * 'size' -- the length of text fields |
40 | 41 | * 'filter-callback -- a function name to give you the chance to |
41 | 42 | * massage the inputted value before it's processed. |
42 | 43 | * @see HTMLForm::filter() |
— | — | @@ -111,14 +112,17 @@ |
112 | 113 | $this->mFlatFields = array(); |
113 | 114 | |
114 | 115 | foreach( $descriptor as $fieldname => $info ) { |
115 | | - $section = ''; |
116 | | - if ( isset( $info['section'] ) ) |
117 | | - $section = $info['section']; |
| 116 | + $section = isset( $info['section'] ) |
| 117 | + ? $info['section'] |
| 118 | + : ''; |
118 | 119 | |
119 | | - $info['name'] = $fieldname; |
| 120 | + $info['name'] = isset( $info['name'] ) |
| 121 | + ? $info['name'] |
| 122 | + : $fieldname; |
120 | 123 | |
121 | | - if ( isset( $info['type'] ) && $info['type'] == 'file' ) |
| 124 | + if ( isset( $info['type'] ) && $info['type'] == 'file' ){ |
122 | 125 | $this->mUseMultipart = true; |
| 126 | + } |
123 | 127 | |
124 | 128 | $field = self::loadInputFromParameters( $info ); |
125 | 129 | $field->mParent = $this; |
— | — | @@ -219,8 +223,9 @@ |
220 | 224 | function trySubmit() { |
221 | 225 | # Check for validation |
222 | 226 | foreach( $this->mFlatFields as $fieldname => $field ) { |
223 | | - if ( !empty( $field->mParams['nodata'] ) ) |
| 227 | + if ( !empty( $field->mParams['nodata'] ) ){ |
224 | 228 | continue; |
| 229 | + } |
225 | 230 | if ( $field->validate( |
226 | 231 | $this->mFieldData[$fieldname], |
227 | 232 | $this->mFieldData ) |
— | — | @@ -290,9 +295,10 @@ |
291 | 296 | * Add a hidden field to the output |
292 | 297 | * @param $name String field name |
293 | 298 | * @param $value String field value |
| 299 | + * @param $attribs Array |
294 | 300 | */ |
295 | | - public function addHiddenField( $name, $value ){ |
296 | | - $this->mHiddenFields[ $name ] = $value; |
| 301 | + public function addHiddenField( $name, $value, $attribs=array() ){ |
| 302 | + $this->mHiddenFields[ $name ] = array( $value, $attribs ); |
297 | 303 | } |
298 | 304 | |
299 | 305 | public function addButton( $name, $value, $id=null, $attribs=null ){ |
— | — | @@ -367,7 +373,8 @@ |
368 | 374 | $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n"; |
369 | 375 | |
370 | 376 | foreach( $this->mHiddenFields as $name => $value ){ |
371 | | - $html .= Html::hidden( $name, $value ) . "\n"; |
| 377 | + list( $value, $attribs ) = $value; |
| 378 | + $html .= Html::hidden( $name, $value, $attribs ) . "\n"; |
372 | 379 | } |
373 | 380 | |
374 | 381 | return $html; |
— | — | @@ -592,8 +599,9 @@ |
593 | 600 | $fieldData = array(); |
594 | 601 | |
595 | 602 | foreach( $this->mFlatFields as $fieldname => $field ) { |
596 | | - if ( !empty( $field->mParams['nodata'] ) ) continue; |
597 | | - if ( !empty( $field->mParams['disabled'] ) ) { |
| 603 | + if ( !empty( $field->mParams['nodata'] ) ){ |
| 604 | + continue; |
| 605 | + } elseif ( !empty( $field->mParams['disabled'] ) ) { |
598 | 606 | $fieldData[$fieldname] = $field->getDefault(); |
599 | 607 | } else { |
600 | 608 | $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest ); |
— | — | @@ -605,7 +613,7 @@ |
606 | 614 | $field = $this->mFlatFields[$name]; |
607 | 615 | $value = $field->filter( $value, $this->mFlatFields ); |
608 | 616 | } |
609 | | - |
| 617 | + |
610 | 618 | $this->mFieldData = $fieldData; |
611 | 619 | } |
612 | 620 | |
— | — | @@ -642,6 +650,7 @@ |
643 | 651 | public $mParams; |
644 | 652 | protected $mLabel; # String label. Set on construction |
645 | 653 | protected $mID; |
| 654 | + protected $mClass = ''; |
646 | 655 | protected $mDefault; |
647 | 656 | public $mParent; |
648 | 657 | |
— | — | @@ -660,7 +669,7 @@ |
661 | 670 | * field input. Don't forget to call parent::validate() to ensure |
662 | 671 | * that the user-defined callback mValidationCallback is still run |
663 | 672 | * @param $value String the value the field was submitted with |
664 | | - * @param $alldata $all the data collected from the form |
| 673 | + * @param $alldata Array the data collected from the form |
665 | 674 | * @return Mixed Bool true on success, or String error to display. |
666 | 675 | */ |
667 | 676 | function validate( $value, $alldata ) { |
— | — | @@ -749,6 +758,10 @@ |
750 | 759 | $this->mID = $id; |
751 | 760 | } |
752 | 761 | |
| 762 | + if ( isset( $params['cssclass'] ) ) { |
| 763 | + $this->mClass = $params['cssclass']; |
| 764 | + } |
| 765 | + |
753 | 766 | if ( isset( $params['validation-callback'] ) ) { |
754 | 767 | $this->mValidationCallback = $params['validation-callback']; |
755 | 768 | } |
— | — | @@ -776,13 +789,19 @@ |
777 | 790 | } |
778 | 791 | |
779 | 792 | $html = $this->getLabelHtml(); |
780 | | - $html .= Html::rawElement( 'td', array( 'class' => 'mw-input' ), |
781 | | - $this->getInputHTML( $value ) ."\n$errors" ); |
| 793 | + $html .= Html::rawElement( |
| 794 | + 'td', |
| 795 | + array( 'class' => 'mw-input' ), |
| 796 | + $this->getInputHTML( $value ) ."\n$errors" |
| 797 | + ); |
782 | 798 | |
783 | 799 | $fieldType = get_class( $this ); |
784 | 800 | |
785 | | - $html = Html::rawElement( 'tr', array( 'class' => "mw-htmlform-field-$fieldType" ), |
786 | | - $html ) . "\n"; |
| 801 | + $html = Html::rawElement( |
| 802 | + 'tr', |
| 803 | + array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass}" ), |
| 804 | + $html |
| 805 | + ) . "\n"; |
787 | 806 | |
788 | 807 | $helptext = null; |
789 | 808 | if ( isset( $this->mParams['help-message'] ) ) { |
— | — | @@ -929,6 +948,16 @@ |
930 | 949 | |
931 | 950 | return Html::element( 'input', $attribs ); |
932 | 951 | } |
| 952 | + |
| 953 | + public function validate( $value, $alldata ){ |
| 954 | + $p = parent::validate( $value, $alldata ); |
| 955 | + if( $p !== true ) return $p; |
| 956 | + |
| 957 | + if( isset( $this->mParams['required'] ) && $value === '' ){ |
| 958 | + return wfMsgExt( 'htmlform-required', 'parseinline' ); |
| 959 | + } |
| 960 | + return true; |
| 961 | + } |
933 | 962 | } |
934 | 963 | class HTMLTextAreaField extends HTMLFormField { |
935 | 964 | |
— | — | @@ -1018,7 +1047,9 @@ |
1019 | 1048 | |
1020 | 1049 | if ( $p !== true ) return $p; |
1021 | 1050 | |
1022 | | - if ( intval( $value ) != $value ) { |
| 1051 | + if ( $value !== '' |
| 1052 | + && ( !is_numeric( $value ) || round( $value ) != $value ) ) |
| 1053 | + { |
1023 | 1054 | return wfMsgExt( 'htmlform-int-invalid', 'parse' ); |
1024 | 1055 | } |
1025 | 1056 | |
— | — | @@ -1223,7 +1254,7 @@ |
1224 | 1255 | } else { |
1225 | 1256 | $thisAttribs = array( 'id' => $this->mID . "-$info", 'value' => $info ); |
1226 | 1257 | |
1227 | | - $checkbox = Xml::check( $this->mName . '[]', in_array( $info, $value ), |
| 1258 | + $checkbox = Xml::check( $this->mName . '[]', $info === $value, |
1228 | 1259 | $attribs + $thisAttribs ); |
1229 | 1260 | $checkbox .= ' ' . Html::rawElement( 'label', array( 'for' => $this->mID . "-$info" ), $label ); |
1230 | 1261 | |
— | — | @@ -1352,9 +1383,14 @@ |
1353 | 1384 | class HTMLHiddenField extends HTMLFormField { |
1354 | 1385 | |
1355 | 1386 | public function getTableRow( $value ){ |
| 1387 | + $params = array(); |
| 1388 | + if( $this->mID ){ |
| 1389 | + $params['id'] = $this->mID; |
| 1390 | + } |
1356 | 1391 | $this->mParent->addHiddenField( |
1357 | | - $this->mParams['name'], |
1358 | | - $this->mParams['default'] |
| 1392 | + $this->mName, |
| 1393 | + $this->mDefault, |
| 1394 | + $params |
1359 | 1395 | ); |
1360 | 1396 | return ''; |
1361 | 1397 | } |