Merge "Deleting a page and then immediately create-protecting it caused a PHP Fatal...
[lhc/web/wiklou.git] / includes / htmlform / HTMLRadioField.php
1 <?php
2
3 /**
4 * Radio checkbox fields.
5 */
6 class HTMLRadioField extends HTMLFormField {
7 function validate( $value, $alldata ) {
8 $p = parent::validate( $value, $alldata );
9
10 if ( $p !== true ) {
11 return $p;
12 }
13
14 if ( !is_string( $value ) && !is_int( $value ) ) {
15 return false;
16 }
17
18 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
19
20 if ( in_array( $value, $validOptions ) ) {
21 return true;
22 } else {
23 return $this->msg( 'htmlform-select-badoption' )->parse();
24 }
25 }
26
27 /**
28 * This returns a block of all the radio options, in one cell.
29 * @see includes/HTMLFormField#getInputHTML()
30 *
31 * @param $value String
32 *
33 * @return String
34 */
35 function getInputHTML( $value ) {
36 $html = $this->formatOptions( $this->mParams['options'], $value );
37
38 return $html;
39 }
40
41 function formatOptions( $options, $value ) {
42 $html = '';
43
44 $attribs = array();
45 if ( !empty( $this->mParams['disabled'] ) ) {
46 $attribs['disabled'] = 'disabled';
47 }
48
49 # @todo Should this produce an unordered list perhaps?
50 foreach ( $options as $label => $info ) {
51 if ( is_array( $info ) ) {
52 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
53 $html .= $this->formatOptions( $info, $value );
54 } else {
55 $id = Sanitizer::escapeId( $this->mID . "-$info" );
56 $radio = Xml::radio( $this->mName, $info, $info == $value, $attribs + array( 'id' => $id ) );
57 $radio .= '&#160;' . Html::rawElement( 'label', array( 'for' => $id ), $label );
58
59 $html .= ' ' . Html::rawElement(
60 'div',
61 array( 'class' => 'mw-htmlform-flatlist-item' ),
62 $radio
63 );
64 }
65 }
66
67 return $html;
68 }
69
70 protected function needsLabel() {
71 return false;
72 }
73 }